Home > C# > Learn C# – Handling Exceptions(3) – Throwing Exceptions

Learn C# – Handling Exceptions(3) – Throwing Exceptions

  1. Handling Exceptions – Part 1 – Basic Try / Catch
  2. Handling Exceptions – Part 2 – Catch block
  3. Handling Exceptions – Part 3 – Throwing Exceptions
  4. Handling Exceptions – Part 4 – Custom Exceptions

Untill now we saw many examples where unprotected code can lead to exceptions. Those exceptions was thrown by the CLR in a result of some wrong input or action. Let’s try to control a little bit more on the process of throwing the exceptions – not only the CLR can do it!

Throwing an exception means “let others know that something wrong happen”. The syntax is pretty simple as well: just use throw, like this:

void Divide (int x, int y)
{
if (y == 0)
throw new ArgumentException();
}

But what we’re throwing? Actually that’s pretty simple too: we must throw an instance of some exception class. In the above example, we’re throwing a new instance of the ArgumentException class, that inherit the base Exception class. Of course, we can also write the same code like this:

void Divide (int x, int y)
{
if (y == 0)
    {
      ArgumentException ex = new ArgumentException();
      throw ex;
    }
}

At this stage I don’t really care who’s handle the exception. I just want to “clear my table” and make sure that I’ve published my problem well. The person who call the code, and in that case – the method caller, should be responsible to handle any exception properly.

One word about bubbeling

A very important behavior of exceptions: from the moment exception was thrown, he only looks for handler. As soon as he will find one everything will come to his place. But, as long as the exception couldn’t find any relevant handler (catch block) – he will keep look for one direction up. Means that if the exception was thrown outside try block, the exception will look for handler in the method caller, and in his caller, and so on untill he gets to the operating system, means crashing.

Throw inside catch block

Sometimes even though we already caught the exception, we will want to throw it again, since we need to handle it again on another place. We can throw the same exception from the catch block, or throw new one.

try
{
  int x = 0;
  int y = 10 / x;
  Console.WriteLine("Done");
}
catch (DivideByZeroException ex)
{
  Console.WriteLine("Error!!");
  throw;//throws the same exception.
  //another version for this will be "throw ex;"
}
try
{
  int x = 0;
  int y = 10 / x;
  Console.WriteLine("Done");
}
catch (DivideByZeroException ex)
{
  Console.WriteLine("Error!!");
  throw new Exception(); // look for another handler
}

In the next post we’ll look more closely into the Exception class, and try create one of ourselves.

Advertisements
Categories: C#
  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: