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
This commit is contained in:
committed by
Christopher McCormack
parent
c00de2b4fb
commit
9a496d687e
@ -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.
|
||||
|
||||
|
Reference in New Issue
Block a user