Exception handling allows programmers to deal with unexpected situations that may occur in programs.
As an example, consider opening a file using the StreamReader class in the System.IO namespace.
To see what kinds of exceptions this class may throw, you can hover the cursor over the class name in Visual Studio.
For instance, the FileNotFoundException.
If this or any other exception occurs, the program will terminate with an error message.
To avoid crashing the program the exceptions must be caught using a try-catch statement.
This statement consists of a try block containing the code that may cause the exception, and one or more catch clauses.
If the try block successfully executes, the program will then continue running after the try-catch statement.
However, if an exception occurs the execution will then be passed to the first catch block able to handle that exception type.
Since the catch block here is not set to handle any specific exception it will catch all of them.
This is equivalent to catching the System.Exception class, because all exceptions derive from this class.
To catch a more specific exception the catch block needs to be placed before more general exceptions.
The catch block can optionally define an exception object,
which can be used to obtain more information about the exception, such as a description of the error.
As the last clause in the try-catch statement, a finally block can be added.
This block is used to clean up certain resources allocated in the try block.
Typically, limited system resources and graphical components need to be released in this way, once they are no longer needed.
The code in the finally block will always execute, whether or not there is an exception.
This will be the case even if the try block ends with a jump statement, such as return.
In this example, the file opened in the try block should be closed if it was successfully opened.
To be able to access the StreamReader object from the finally clause, it must be declared outside of the try block.
Keep in mind that if you forget to close the stream, the garbage handler will eventually close it for you,
but it is a good practice to do it yourself.
The statement here is known as a try-catch-finally statement.
The catch blocks can be left out to create a try-finally statement.
This statement will not catch any exceptions.
Instead, it will ensure the proper disposal of any resources allocated in the try block.
This can be useful if the allocated resource does not throw any exceptions.
One such class would be Bitmap, in the System.Drawing namespace.
Note that when using a Console Project a reference to the System.Drawing assembly needs to be
manually added for those members to be accessible.
To do so, right click the References folder in the Solution Explorer window, and select Add Reference.
Then from the .NET tab select the System.Drawing assembly, and click OK to add its reference to your project.
The using statement provides a simpler syntax for writing the try-finally statement.
This statement starts with the using keyword followed by the resource to be acquired, specified in parentheses.
It then includes a code block in which the obtained resource can be used.
When the code block has finished executing, the Dispose method of the object is automatically called to clean it up.
This method comes from the System.IDisposable interface, so the specified resource must implement this interface.
Note that the code here performs the same function as the try-finally example shown before, but with fewer lines of code.
When a situation occurs that a method cannot recover from,
it can generate an exception to signal the caller that the method has failed.
This is done using the throw keyword, followed by a new instance of a class deriving from System.Exception.
The exception will then propagate up the caller stack, until it is caught.
If a caller catches the exception but is not able to recover from it,
the exception can be re-thrown using only the throw keyword.
If there are no more try-catch statements the program will stop executing and display the error message.