In a catch block that specifies a particular exception type to catch, you’ll typically include a named variable representing the exception object that is caught. Using this variable, code within your handler can access information about the exception that was caught.
try { Dog d = new Dog("Kirby", 15); d.Bark(BarkSound.Woof, 99); } catch (DogBarkException dbe) { Console.WriteLine("Kirby didn't bark properly"); Console.WriteLine(string.Format("Can't bark {0} times", dbe.NumTimes)); }
If you don’t need to access data within the exception object, but you still want to catch exceptions of a specific type, you can omit the variable. This will allow you to catch exceptions of a particular type, but not give you any access of data within the exception object.
try { Dog d = new Dog("Kirby", 15); d.Bark(BarkSound.Woof, 99); } catch (DogBarkException) { Console.WriteLine("Kirby didn't bark properly"); }
Filed under: Exceptions Tagged: C#, catch, Exceptions Image may be NSFW.
Clik here to view.

Clik here to view.
