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
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With