Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finally block executing before catch

Tags:

c#

try-catch

As far as I understand try-catch-finally statement, just after exception is caught finally block gets executed. How does this apply when function throws exception, like example below. What if some resources are released in finally block which by initial exception could not be. Does this mean that finally block throws new (another) exception, overriding original exception.

I know that you can catch the exception that might be thrown in the try block of a try-finally statement higher up the call stack. That is, you can catch the exception in the method that calls the method that contains the try-finally statement (msdn documentation).

static void foo()   
{
  try   
  {
    Console.WriteLine("foo");
    throw new Exception("exception");   
  }   
  finally   
  {
    Console.WriteLine("foo's finally called");   
  }   
}

static void Main(string[] args)
{
  try
  {
    foo();
  }
  catch (Exception e)
  {
    Console.WriteLine("Exception caught");
  }
}

Output:

foo
foo's finally called
Exception caught
like image 987
broadband Avatar asked May 22 '14 10:05

broadband


People also ask

Can we place a Finally block before catch block?

To do this, you can use a finally block. A finally block always executes, regardless of whether an exception is thrown. The following code example uses a try / catch block to catch an ArgumentOutOfRangeException. The Main method creates two arrays and attempts to copy one to the other.

When finally block executed in try catch finally?

catch statement is comprised of a try block and either a catch block, a finally block, or both. The code in the try block is executed first, and if it throws an exception, the code in the catch block will be executed. The code in the finally block will always be executed before control flow exits the entire construct.

Can finally come before catch in Java?

When you are using try-catch-finally. If your code get any error then it goes to first catch and then finally. If your code does not threw any error then it will at last calls the finally and then further goes for execution.

Will finally execute after catch?

Yes, the finally block will be executed even after a return statement in a method. The finally block will always execute even an exception occurred or not in Java.


1 Answers

As far as I understand try-catch-finally statement, just after exception is caught finally block gets executed.

No, that's wrong.

The finally block gets executed after either an applicable catch clause block has been executed, or immediately after the exception is thrown if no applicable catch exists. However, that only applies to catch and finally on the same try block -- if the exception is propagated, finally will run before any catch and/or finally that is further up the call stack.

How does this apply when function throws exception, like example below.

foo is called, it throws, and its finally block is executed. Since there was no catch the exception propagates and is caught inside main.

What if some resources are released in finally block which by initial exception could not be.

I do not understand the question.

Does this mean that finally block throws new (another) exception, overriding original exception.

Yes, that's how it works.

like image 157
Jon Avatar answered Nov 02 '22 11:11

Jon