What happens if a final block throws an exception?
OR
How to handle exceptions that occur in a final block?
Three things happen.

Let's understand with an example.
Step 1. First, we will create a console application named ExceptioninFinallyblock.

Step 2. Now here, we will create a method named FinallyBlock and include a try, catch, and finally block.
Now we write some code within the final block that would cause an exception to occur with the following code.
using System;
namespace ExceptioninFinallyblock
{
class Program
{
static void Main(string[] args)
{
FinallyBlock(); // Calling the method
}
public static void FinallyBlock()
{
try
{
}
catch
{
}
finally
{
int result = Convert.ToInt32("THREE"); // Convert to integer
}
}
}
}
Step 3. So notice that at the higher level, where we called the FinallyBlock(), the method doesn't have any exception handling mechanism there. When we run the program, the application will terminate with the exception that occurs in the final block.

Now we will provide an exception-handling mechanism at the higher level in the Main() method, where we are actually calling the FinallyBlock() method. So we are using the exception handling mechanism.
Here we have the opportunity to handle the exception that occurs in the final block of the FinallyBlock() method. Here we write an exception message to the console with the following code.
using System;
namespace ExceptioninFinallyblock
{
class Program
{
static void Main(string[] args)
{
try
{
FinallyBlock(); // Calling the method
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
public static void FinallyBlock()
{
try
{
}
catch
{
}
finally
{
try
{
int result = Convert.ToInt32("THREE"); // Convert to integer
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
}
Now run the program. We will see that the application does not crash since we have an exception-handling mechanism at a higher level. We have the opportunity to handle the exception.

Step 4. So what is the second point?

So at the final block where the exception is thrown, let's include a Console.WriteLine() statement. That line is present before the exception occurs, so this piece of code will execute.






shashi mishraPosted Apr 18, 2018, 9:27 AM
Thank you so much for nice example,
Parth KalePosted Oct 4, 2017, 3:40 PM
This helped me a lot thanks ..
Upendra Pratap ShahiPosted Sep 12, 2017, 1:21 PM
Nice one explanation..
Ramdas MutkulePosted Apr 10, 2017, 6:02 AM
Very nice explain thanks...
SubashPosted Mar 7, 2017, 12:13 PM
Very good explanation thanx
Manas MohapatraPosted Sep 26, 2015, 2:30 PM
Nice one...
Shweta LodhaPosted Jan 4, 2015, 12:21 AM
Nice
Jitendra KumarPosted Jan 3, 2015, 2:18 PM
Nice one.
Guest UserPosted Jan 3, 2015, 11:11 AM
Good one! At times rather using highest level of abstraction, i.e., Exception class we can use the a more focussed one ex- Overflow exception.
Sourabh SomaniPosted Jan 3, 2015, 8:54 AM
Nice article keep it up
Sourabh SomaniPosted Jan 3, 2015, 8:54 AM
Nice article keep it up
Shaili DashoraPosted Jan 3, 2015, 5:02 AM
Thank u...
Vithal WadjePosted Jan 3, 2015, 3:26 AM
nice work keep it up