C# ArgumentException
Program.cs
int i = 0;
string s = "Hello, World!";
Console.WriteLine(s.CompareTo(i));
Output
kodingwindow@kw:~/csharp$ dotnet run
Unhandled exception. System.ArgumentException: Object must be of type String.
    at System.String.CompareTo(Object value)
    at Program.<Main>$(String[] args) in /home/kodingwindow/csharp/Program.cs:line 3
kodingwindow@kw:~/csharp$ 
C# program to handle the ArgumentException
Program.cs
int i = 0;
string s = "Hello, World!";
try
{
    Console.WriteLine(s.CompareTo(i));
}
catch(ArgumentException)
{
    Console.WriteLine("Something went wrong...");
}
Output
kodingwindow@kw:~/csharp$ dotnet run
Something went wrong...
kodingwindow@kw:~/csharp$ 
Advertisement