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.
1
2
3
4
5
6
7
8
9
10
| try{ Dog d = new Dog("Kirby", 15); d.Bark(<span class="skimlinks-unlinked">BarkSound.Woof</span>, 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.
1
2
3
4
5
6
7
8
9
| try{ Dog d = new Dog("Kirby", 15); d.Bark(<span class="skimlinks-unlinked">BarkSound.Woof</span>, 99);}catch (DogBarkException){ Console.WriteLine("Kirby didn't bark properly");} |

