Exception/Error Handling in C# - Part 1


Introduction

In programming, exceptions/errors can happen (appear) at any time and by using traditional techniques to manually add error-detecting code around every statement is cumbersome, time consuming. C# makes it easy to separate the error-handling code from the code that implements the main flow of the program by using exceptions and exception handlers. To write exception aware programs, we need to do two things:

(i) By placing statements inside a try block

All statements placed inside try block are executed and if none of the exception appears, run all codes one by one till end. If any error condition appears, execution jumps out from try block in catch block.

(ii) By using one or more catch handlers

If anyone statement in try block generates exception, then runtime examines the appropriate catch handler and jumps to that.

Example

            try
            {
                //statements
            }
            catch (OverflowException oEx)
            {
                Label1.Text = oEx.Message;
            }
            catch (FormatException fEx)
            {
                Label1.Text = fEx.Message;
            }
            catch (Exception ex)
            {
                Label1.Text = ex.Message;
            }

In above example, I am using multiple catch handlers. The first one, OverflowException appears when we try to handle some value which outside the range of Data Type. The second one, FormatException appears when we try to calculate (mathematical task) on some string value without parse it to corresponding numeric Data Type. You must be knowing, multiplication can only be possible for numeric Data Types, or even if we need to multiply to strings then we should parse it first otherwise we will receive FormatException. The third one, Exception executes when none of above (OverflowException and FormatException) exception satisfied.

            catch (Exception ex)  //general catch handler
            {
                Label1.Text = ex.Message;
            }

Above exception is universal exception, the exception-catching mechanism provided by C# and the Microsoft .NET Framework is quite comprehensive. The .NET Framework defines many types of exceptions, and any programs we write can throw most of them. It is highly unlikely that we’ll want to write catch handlers for every possible exception that our code can throw. The answer to this question lies in the way the different exceptions are related to one another. Exceptions are organized into families called inheritance hierarchies FormatException and OverfowException both belong to a family called SystemException, as do a number of other exceptions. SystemException is itself a member of a wider family simply called Exception, which is the great-granddaddy of all exceptions. If we catch Exception, the handler traps every possible exception that can occur.

If we want to catch Exception, we can actually omit its name from the catch handler because it is the default exception:

            catch
            {
                //statements
            }

However, this is not always recommended. The exception object passed in to the catch handler can contain useful information concerning the exception, which is not accessible when using above catch handler.

Matching Multiple Exceptions

One question here, what happens when same exception matches multiple catch handlers at the end of a try block? If we catch FormatException/OverflowException and Exception in two different handlers, which one will run (or will both execute)? The answer is, when an exception occurs, the first handler found by the runtime that matches the exception is used, and the others are ignored. One more question here, what happens when we place a handler for Exception before a handler for FormatException/OverflowException, the FormatException/OverflowException handler will never run. It will show error. Therefore, we should place more specific catch handlers above a general catch handler after a try block. If none of the specific catch handlers matches the exception, the general catch handler will.

Further Reading

Comments

Popular posts from this blog

Migrating database from ASP.NET Identity to ASP.NET Core Identity

Customize User's Profile in ASP.NET Identity System

Lambda two tables and three tables inner join code samples