Exceptions and Exception Handling in C# – Programming, Pseudocode Example, C# Programming Example

Exceptions and Exception Handling in C#

So far, we have never talked about error handling in C #. Simplifying, an error is the occurrence of an unexpected situation when running a program.

Errors in the .NET world, in the best object-oriented tradition (Java, C ++, etc.) are represented with exceptions: an entire class hierarchy for handling unwanted events.

Exceptions are “displayed” when an abnormal situation occurs, such as an error. You can ignore them, with the risk of having unpleasant surprises at run time, or you can intercept and manage them.

This involves the use of a construct created specifically for intercepting exceptions: try-catch-finally.


If the code running in the try block raises an exception, it is caught and the catch block is executed. In all cases, the finally block is also executed.

In the try-catch construct, you must specify at least one catch or a finally clause; you can specify more captures, one for each type of error you want to handle, while you can only insert a finally clause.

In the catch clause, you can specify the type of exception you want to handle; for example :

The first block is executed if an overflow error occurs, for example when trying to assign a variable to a value outside its definition. The second is invoked if a division is made by 0.

In both cases, the “ex” object contains various information about the error. For example, the ex.Message property returns a string describing the cause of the exception.

Let’s now update the application we did in the previous lesson by adding error handling. In particular, we consider the instruction

In case the specified file does not exist, the FromFile method raises an exception of type FileNotFoundException. So insert this statement in a try … catch block:

Since exceptions are also classes, they are also grouped in the namespace. For example, for the Framework to know where to look for the FileNotFoundException class, the “using” clause should be added to the “System.IO” namespace.

Whenever you try to load a file, a “FileNotFoundException” exception occurs, the corresponding catch block is executed, which displays a message box. The file error not found, however, is not the only one that can happen when trying to open a file: for example, if the available memory is not enough to complete the download, you get an error like OutOfMemoryException.

Again, if you press the “Show” button without typing any file names, an ArgumentException exception occurs.

Then we add a second catch block to handle all other cases, getting:

Because Exception is the base class from which all other exceptions are derived, the second catch block is executed whenever the FromFile method throws an exception that is not of type “FileNotFoundException” (and not do not drift).

In this case, we used the information contained in the “ex” exception to find out exactly where the error occurred.

It is always best to avoid the treatment of exceptions, trying to predict, as far as possible, the causes that can lead to an unexpected situation: an abuse of try … catch, in fact, can weigh in a program. For example, in our application, the ArgumentException error can always be avoided simply by invoking the FromFile method only if something has been typed in the text box.


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.