Lets Learn

Opinion Matters

  • Subscribe

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 8 other followers

  • Blog Stats

    • 74,660 hits
  • Ankush

    Error: Twitter did not respond. Please wait a few minutes and refresh this page.

Question – 5 : Make sure Exceptions Are Not Lost in Finally Blocks

Posted by Ankush on February 21, 2011


Let’s say you have an exception and you have added a finally block. Excetion will be lost and you will not not know what caused the excetion, it will be lost. And You want to protect against losing exception information when using multiple trycatch-finally blocks.

Hint : This really helps when you have multiple try/catch handler and you want to correctly rethrow the exception so that it bubblesup to the outer exception handler.

Do you have any suggestions which you can leave here as a part of the comment?

Advertisements

8 Responses to “Question – 5 : Make sure Exceptions Are Not Lost in Finally Blocks”

  1. ajay said

    use ‘throw’ statement to throw the exception. Note that there is a difference beween ‘throw’ and ‘throw ‘.

    throw – throws the exception (the stack trace remains unchanged)
    throw – throws the exception, however the stack trace is modified.

    Consider the following code snippet:

    private void button1_Click(object sender, EventArgs e)
    {
    try
    {
    Function1();
    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.StackTrace);
    }
    }
    private void Function1()
    {
    Function2();
    }
    private void Function2()
    {
    try
    {
    Function3();
    }
    catch (Exception ex)
    {
    throw;
    }
    }

    private void Function3()
    {
    int i = 0;
    int j = 10;

    Function4(i,j);
    }
    private void Function4(int i, int j)
    {
    try
    {
    int result = j/i;
    }
    catch(Exception ex)
    {
    throw;
    }
    }

    In the above code, i am throwing exception from Function4. I am also catching the exception in Function2 and i am displaying the stack trace in Button1_Click method. Because i am using throw statement to throw the exception stack trace give me information that the exception occured at Function4. However if i use following code in Function2(throw ex):

    private void Function2()
    {
    try
    {
    Function3();
    }
    catch (Exception ex)
    {
    throw ex;
    }
    }

    messagebox diplays the that exception occured in Function2().

  2. ajay said

    Some modification to my previous comment:

    use ‘throw’ statement to throw the exception. Note that there is a difference beween ‘throw’ and ‘throw ex‘.

    throw – throws the exception (the stack trace remains unchanged)
    throw ex – throws the exception, however the stack trace is modified.

  3. Ankush said

    well…you missed the point.

    Lets say I have 3 try catch block. And innermost has try catch.finally blick now the exception will be lost when it will enter into the finally block and I want to make sure that it bubbles up to outermost exception once it executes the code in finally block.

    What should I do?

  4. Ankush said

    well.its simple..consider this snippet

    class Program
    {
    private static void ShowError()
    {
    try
    {
    Console.WriteLine(“In inner try”);
    int z2 = 9999999;
    checked { z2 *= 999999999; }
    }
    catch (OverflowException ofe)
    {
    Console.WriteLine(“An Overflow occurred. Error message: ” + ofe.Message);
    throw;
    }
    catch (Exception e)
    {
    Console.WriteLine(“Another type of error occurred. ” + “Error message: ” + e.Message);
    throw;
    }
    finally
    {
    Console.WriteLine(“In inner finally”);
    throw (new Exception(“Oops”));
    }
    }

    static void TestMethod()
    {
    try
    {
    Console.WriteLine(“In outer try”);
    ShowError();
    }
    catch (Exception e)
    {
    Console.WriteLine(“In outer catch. Caught exception: ” + e.Message);
    }
    finally
    {
    Console.WriteLine(“In outer finally”);
    }
    }
    static void Main(string[] args)
    {
    TestMethod();
    }
    }

    This program generates an OverflowException which I would like to bubble up to TestMethod but if you see the exception gets changed when I try to get the exception.Message. If changes to what I throw in the inner fiannly block.

    So, my question, what I can do to make this exception bubbles up properly in these kind of situations?

    • ajay said

      Got it. So the question in simple terms is:

      When an exception is thrown from the finally block, it eats/hides the original exception(if any).Is there a way to make the original exception bubble up?

      I couldn’t find the answer of this one. Waiting for the answer :).

  5. Ankush said

    well..just add a try catch in finally block and everything will work fine!!!

    Cheers!
    Ankush

  6. I simply want to mention I am all new to weblog and absolutely loved this web blog. Most likely I’m want to bookmark your website . You certainly have great posts. Thank you for sharing your blog site.

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: