C#C
C#12mo ago
84 replies
Faker

Exception Handling in C#

Hello guys, sorry to disturb you all; consider the following code:

try
{
    // Step 1: code execution begins
    try
    {
        // Step 2: an exception occurs here
    }
    finally
    {
        // Step 4: the system executes the finally code block associated with the try statement where the exception occurred
    }

}
catch // Step 3: the system finds a catch clause that can handle the exception
{   
   // Step 5: the system transfers control to the first line of the catch code block
}


I have a question. I know that try-catch blocks are used to handle exceptions and the finally block is used to "release" any resource after the catch block has been executed. The finally block is always executed irrespective if their is an error or not.

My question is, in the above code, notice the inner try-finally statement doesn't have a catch block. So if their is an error in this try block, what happens? We noticed that their is no catch block in the inner try block, so we start looking in the outer try block? What about the finally block, we don't execute it until we finish scheming in the outer try-block? Does this mean that any catch block will always run before any finally block?
Was this page helpful?