Home > C#, Learn C# > Learn C# – Handling Exceptions – Part 2

Learn C# – Handling Exceptions – Part 2

  1. Handling Exceptions – Part 1 – Basic Try / Catch
  2. Handling Exceptions – Part 2 – Catch block
  3. Learn C# – Exceptions Handling – Part 3 – Throwing Exceptions

In this part we goanna look in more depth in the try/catch clauses.

The simple case of try/catch, as we seen in the last post, can catch any exception occurred in the try block, and let us handle it in our catch block.

try
{
  int x = 0;
  int y = 10 / x;
  Console.WriteLine("Done");
}
catch
{
  Console.WriteLine("Error!!");
}
//OUTPUT:Error!!
//Notice: "Done" wasn't printed to the console, since the exception was thrown before.

Let’s see some other options available for the try / catch.

Exception details

Not only the catch block can catch our exceptions, but also supply details about the caught exception, like this:

try
{
  //some exception
}
catch (Exception ex)
{
  //ex is an instance of the Exception class, with properties that supply many details about the exception.
}

The Exception class is the base class for any exception used in the .NET framework, and will be described in details later. For now, just note that this class contains some properties with details about the exception, as error message, where the exception originally occurred, etc.

Handle specific exception

The Exception class is not the only class in .NET used to handle exceptions. Lot’s of other classes are used, and all share one important behavior: They all inherit (directly or indirectly) from the base Exception class. NullReferenceException, DivideByZeroException and many others are all classes that have the same goal – handling exceptions. the difference between them is only the exception type, and sometimes, the details supplied.

specifying specific exception class in the brackets after the catch keyword will result catching only exceptions from this specific type. catch block without any type defined – will catch any exception. For example:

try
{
  int x = 0;
  int y = 10 / x;
}
catch (DivideByZeroException ex)
{
  Console.WriteLine("Error!!");
}
//OUTPUT:Error!!

In the above example, any other exception do not get caught (and if the program won’t find any suitable exception handler – it will probably crash). Notice that unlike method parameters, the ex used as a parameter name for the DivideByZeroException instance is not mandatory, and if you not going to use the ex properties, but only want to limit the exception types been caught, you don’t need to declare ex.

Handling some specific exceptions

The above technique of handling one specific exception, can be multiplied to more than one specific exception, like this:

try
{
 int x = int.Parse(Console.ReadLine());
 int y = 10 / x;
}
catch (DivideByZeroException ex)
{
 Console.WriteLine("Divide by zero exception was caught");
}
catch (FormatException ex)
{
 Console.WriteLine("Format exception was caught");
}

Now we handle two different exceptions – one for the DivideByZero and other for the Format. Whichever occurs first will cause his own catch clause to be executed.

One important thing need to be taken in consideration when declaring multiple exception handlers – The order of the catch clauses. Let’s have a look in some example:

try
{
 int x = int.Parse(Console.ReadLine());
 int y = 10 / x;
}
catch (Exception ex)
{
 Console.WriteLine("Divide by zero exception was caught");
}
catch (FormatException ex)
{
 Console.WriteLine("Format exception was caught");
}

This Example will cause compile-time error. The first catch declares the base Exception class as the type, and the second one declare the FormatException. Since all exception inherit from the base Exception class, any exception that will be thrown in the try block will be considered to be general Exception, so it will be caught in the first catch. Hence, the second catch block will never be called.

In the next post we’ll try to throw some exception by ourselves!

Advertisements
Categories: C#, Learn C# Tags: , , , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: