locked
what happens if there is exception thrown in catch block ...will another catch block catch that exception? have look the below code RRS feed

  • Question

  • User1094269964 posted

    lets say i have function

     public void DoWork()
            {
                try
                {
                    throw CustomException("Testing");
                }
                catch (CustomException ex)      // Catch 1
                {
                    throw new Exception(ex.Message);
                }
                catch (Exception ex) //Catch2
                {
                    /*
                     * Will the exception thrown in Catch 1 will be catched in Catch2 ?
                     */
                }
            }
    Sunday, August 24, 2014 2:32 AM

Answers

  • User-434868552 posted

    @bsurendiran   welcome to forums.asp.net

    if you do not have a try/catch, exceptions will be caught at some higher level in the call stack, or missed altogether and caught by the operating system ~~ worst case scenario, if the operating system can not catch the exception, you likely will crash your computer.

    Think of an exception as a bubble from the bottom of the ocean; as it bubbles up to the surface, it may get trapped on the way up to the top.

    important, exceptions are hierarchical; for that reason, when you have multiple catch blocks, you stack them from more specific to less specific, if you try to stack them the wrong way around, the c# compiler will not let you.

        try
        {	        
        }
        catch (Exception ex)
        {
        }
        catch (DivideByZeroException zdivex)  // error
        {
        }
    error:  A previous catch clause already catches all exceptions
           of this or of a super type ('System.Exception')

    reference:  http://msdn.microsoft.com/en-us/library/5b2yeyab%28v=vs.110%29.aspx
                    "Handling and Throwing Exceptions"

    edit:

    you can throw and/or rethrow exceptions.  see http://www.dotnetperls.com/throw  (generally, you should be careful so as to not mess up your StackTrace data.)

    end edit.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 24, 2014 4:36 AM

All replies

  • User-1360095595 posted

    No. Only the first catch. Only one catch block will be hit. 

    Sunday, August 24, 2014 2:40 AM
  • User-434868552 posted

    @bsurendiran   welcome to forums.asp.net

    if you do not have a try/catch, exceptions will be caught at some higher level in the call stack, or missed altogether and caught by the operating system ~~ worst case scenario, if the operating system can not catch the exception, you likely will crash your computer.

    Think of an exception as a bubble from the bottom of the ocean; as it bubbles up to the surface, it may get trapped on the way up to the top.

    important, exceptions are hierarchical; for that reason, when you have multiple catch blocks, you stack them from more specific to less specific, if you try to stack them the wrong way around, the c# compiler will not let you.

        try
        {	        
        }
        catch (Exception ex)
        {
        }
        catch (DivideByZeroException zdivex)  // error
        {
        }
    error:  A previous catch clause already catches all exceptions
           of this or of a super type ('System.Exception')

    reference:  http://msdn.microsoft.com/en-us/library/5b2yeyab%28v=vs.110%29.aspx
                    "Handling and Throwing Exceptions"

    edit:

    you can throw and/or rethrow exceptions.  see http://www.dotnetperls.com/throw  (generally, you should be careful so as to not mess up your StackTrace data.)

    end edit.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 24, 2014 4:36 AM