System.Exception—catch an ArgumentException

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
C# Code Snippets

C# Source Code Bank

See also …
edit

This C# code snippet first causes and then catches an ArgumentException.

using System;
 
public class ArgumentExceptionExample
{
   public static void Main()
   {
      try
      {
         string s = "C# Online.NET";
         s.CompareTo (true);         // Invalid argument type
      }
      catch (ArgumentException e)
      {
         Console.WriteLine ("Exception: {0}", e);
      }
   }
}


 ArgumentException example (program output)
Exception: System.ArgumentException: Object must be of type String.
at System.String.CompareTo(Object value)
at ArgumentExceptionExample.Main() in Program.cs:line 10




Personal tools