What happens if an exception occurs in Catch block in C#. Also what would be the caller result in that case

39,473

Solution 1

An exception thrown in a catch block will behave the same as an exception thrown without it - it will go up the stack until it is caught in a higher level catch block, if one exists. Doing this is quite normal if you want to change or wrap the original exception; i.e.:

public void MyStartMethod
{
    try
    {
        //do something
        MyBadMethod();
    }
    catch(MySpecialException mse)
    {
        //this is the higher level catch block, specifically catching MySpecialException 
    }
}

public void MyBadMethod()
{
    try
    {
        //do something silly that causes an exception
    }
    catch (Exception e)
    {
        //do some logging

        throw new MySpecialException(e);
    }
}

public class MySpecialException : Exception 
{   
    public MySpecialException(Exception e) { ...etc... }
}

In your case, myResult will have whatever value it had before, if it's even still in scope.

Solution 2

The info below will help (from a previous answer of mine to a related question). If your catch block throws an exception and there are no other catch blocks to handle it besides the one that caused it, it will continue to get re thrown then 'Windows handles it'.

If a exception occurs the CLR traverses up the call stack looking for a matching catch expression. If the CLR doen't finds a matching one, or the Exception gets re thrown each time, the Exception bubbles out of the Main() method. In that case Windows handles the Exception.

Event Handling of Console Applications is the easiest to understand, because there is no special Handling by the CLR. The Exception is leaving the Applications Thread if not caught. The CLR opens a window asking for debug or exit the application. If the user chooses to debug, the debugger starts. If the user chooses to close, the Application exits and the Exception is serialized and written to the console.

Solution 3

An exception in the catch will basically behave as if there was no catch block there to begin with. You see this pattern in multilayered code where you rethrow exceptions. This is a slight variation on your example, but the result is very similar.

try
{}
catch
{
  throw;
}

In the case above and in your case the exception is considered unhandled since it's still propagating up the stack.

There will be no return value. The program simply fails if there is no other catch block to deal with it.

Solution 4

In case if its a child function the exception will be sent to the catch block of calling function

In case if its a main function the exception would be thrown and either handled by a calling method or unhanded

Secondly we don't write anything in catch blocks which can cause an exception.

They are usually used to throw or log exception.

Even if there is something you can use the Finally block so that any occupied resources can be relased.

A common usage of catch and finally together is to obtain and use resources in a try block, deal with exceptional circumstances in a catch block, and release the resources in the finally block.

MSDN DOCUMENTATION

Share:
39,473
Jasmine
Author by

Jasmine

Updated on January 28, 2020

Comments

  • Jasmine
    Jasmine over 4 years

    It was an interview question, quite simple, but I am not confident about the answer.

    What happens if an exception occurs in catch block ?

    I am trying to give an example small prog of what the interviewer was trying to ask me, please correct my program if it is not compiling, I am really new to this. Bottom line is what happens if an exception occurs in Catch and what will be the value of caller int hat case.

    For instance, I have the following:

    double Calculate(int x)
    {
        try
        {
            x = x/2;
        }
        catch(Exception ex)
        {
            Console.Writeline("Message: "+ ex.Message);
        }
        finally
        {
          x = 10;
        }
        return x;
    }
    
    double myResult = Calculate(x); //x can be any number or 0 for example
    

    Now there are two questions:

    1. What happens if an exception happens in catch block ? Also, how to resolve it ? (This is simple example of what the interviewer was asking a similar question).

    2. What will happen to myResult if an exception happens in Calculate(x) method ?What will be its value in all cases ? (Please explain every case with an example)

    I would like to understand this with a detailed explanation too.

    Thank you so much.