Mega Code Archive

 
Categories / C# Book / 01 Language Basics
 

0198 catch statement

The exception type is specified in the catch statement. The exception type can be System.Exception or its subclasses. The following code catches each possible exception type one by one using catch statement. using System; class Test { static void Main(string[] args) { try { byte b = byte.Parse(args[0]); Console.WriteLine(b); } catch (IndexOutOfRangeException ex) { Console.WriteLine("Please provide at least one argument"); } catch (FormatException ex) { Console.WriteLine("That's not a number!"); } catch (OverflowException ex) { Console.WriteLine("You've given me more than a byte!"); } } } The output: Please provide at least one argument