From 9a496d687e7f1c40d475d1a4cdf8bb32c8ea0076 Mon Sep 17 00:00:00 2001 From: Max Dionysius Date: Sun, 18 Nov 2018 02:16:28 +0100 Subject: [PATCH] Added another catch block (#21965) * Added another catch block Added two more catch blocks, so the user can see, he can handle different exception types differently * fixed comment to match catch statement --- guide/english/csharp/try-catch/index.md | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/guide/english/csharp/try-catch/index.md b/guide/english/csharp/try-catch/index.md index c8525c7849..5db6663d6a 100644 --- a/guide/english/csharp/try-catch/index.md +++ b/guide/english/csharp/try-catch/index.md @@ -68,7 +68,7 @@ var parsedUserInput = Int32.Parse(userInput); // Correct ``` ## Catch block -This block is where you specify what type of ```Exception``` you want to catch. If you want to catch ALL possible exceptions you can use the ```Exception``` base class. If you want to only catch a specific type of exception you can specify that instead. Some examples of other exception types are ```ArgumentException```, ```OutOfMemoryException ``` and ```FormatException```. +This block is where you specify what type of ```Exception``` you want to catch. If you want to catch ALL possible exceptions you can use the ```Exception``` base class. If you want to only catch a specific type of exception you can specify that instead. Some examples of other exception types are ```ArgumentException```, ```OutOfMemoryException ``` and ```FormatException```. You can also use the exception types to handle these exceptions different. ```csharp try @@ -78,8 +78,21 @@ try // Only FormatExceptions will be caught in this catch block. catch(FormatException exceptionVariable) { + // DO SOMETHING Console.WriteLine(exceptionVariable.Message); } +// Only ArgumentNullException will be caught in this catch block. +catch(ArgumentNullException nullException) +{ + // DO SOMETHING DIFFERENT + Console.WriteLine(nullException.Message); +} +// Catch every other possible exception +catch(Exception exception) +{ + // DO SOMETHING ELSE + Console.WriteLine(exception.Message); +} ``` The variable declared after the type of exception will contain all the data of the exception and can be used within the ```Catch``` block.