Exception in Finally Block

Question: What happens if a finally block throws an exception?

Or

How to handle exceptions that occur in a finally block?

Answer: Three things happen.

exception

Let's understand with an example

Step 1: first we will create a console application named ExceptioninFinallyblock.

console application

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 finally block that would cause an exception to occur with the following code:

  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Text;    
  5.     
  6. namespace ExceptioninFinallyblock    
  7. {    
  8.     class Program    
  9.     {    
  10.         static void Main(string[] args)    
  11.         {    
  12.             FinallyBlock();//calling method    
  13.         }    
  14.     
  15.         public static void FinallyBlock()    
  16.        {    
  17.            try    
  18.            {    
  19.     
  20.            }    
  21.            catch    
  22.            {    
  23.     
  24.            }    
  25.            finally    
  26.            {    
  27.                int result = Convert.ToInt32("THREE");//convert to integer    
  28.            }    
  29.        }    
  30.     }    
  31. }   
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 finally block.

command prompt

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 finally block of the FinallyBlock() method. Here we write an exception message to the console with the following code.
  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Text;    
  5.     
  6. namespace ExceptioninFinallyblock    
  7. {    
  8.     class Program    
  9.     {    
  10.         static void Main(string[] args)    
  11.         {    
  12.             try    
  13.             {    
  14.                 FinallyBlock();//calling method    
  15.             }    
  16.             catch(Exception ex)    
  17.             {    
  18.                 Console.WriteLine(ex.Message);    
  19.             }    
  20.         }    
  21.     
  22.         public static void FinallyBlock()    
  23.        {    
  24.            try    
  25.            {    
  26.     
  27.            }    
  28.            catch    
  29.            {    
  30.     
  31.            }    
  32.            finally    
  33.            {    
  34.                int result = Convert.ToInt32("THREE");//convert to integer    
  35.            }    
  36.        }    
  37.     }    
  38. }  
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.

output

Step 4:
So what is the second point.

finally

So at the finally 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.
  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Text;    
  5.     
  6. namespace ExceptioninFinallyblock    
  7. {    
  8.     class Program    
  9.     {    
  10.         static void Main(string[] args)    
  11.         {    
  12.             try    
  13.             {    
  14.                 FinallyBlock();//calling method    
  15.             }    
  16.             catch(Exception ex)    
  17.             {    
  18.                 Console.WriteLine(ex.Message);    
  19.             }    
  20.         }    
  21.     
  22.         public static void FinallyBlock()    
  23.        {    
  24.            try    
  25.            {    
  26.     
  27.            }    
  28.            catch    
  29.            {    
  30.     
  31.            }    
  32.            finally    
  33.            {    
  34.                Console.WriteLine("This line wil be executed");//statement before exception occur    
  35.                int result = Convert.ToInt32("THREE");//here will be exception occur    
  36.            }    
  37.        }    
  38.     }    
  39. }   
execute program

Now we will include one Console.WriteLine() statement after the line where the exception occurs so the execution will stop at this line and the Console.WriteLine() statement will not be executed.
  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Text;    
  5.     
  6. namespace ExceptioninFinallyblock    
  7. {    
  8.     class Program    
  9.     {    
  10.         static void Main(string[] args)    
  11.         {    
  12.             try    
  13.             {    
  14.                 FinallyBlock();//calling method    
  15.             }    
  16.             catch(Exception ex)    
  17.             {    
  18.                 Console.WriteLine(ex.Message);    
  19.             }    
  20.         }    
  21.     
  22.         public static void FinallyBlock()    
  23.        {    
  24.            try    
  25.            {    
  26.     
  27.            }    
  28.            catch    
  29.            {    
  30.     
  31.            }    
  32.            finally    
  33.            {    
  34.                Console.WriteLine("This line wil be executed");//statement before exception occur    
  35.                int result = Convert.ToInt32("THREE");//convert to integer    
  36.                Console.WriteLine("This line wil Not be executed");//statement after exception occur    
  37.            }    
  38.        }    
  39.     }    
  40. }    
Now let's run and see the output, the first line is printed that is written before the exception occurs.

And the second line that is written after the exception occur is not printed because at the point where the Exception occured the execution will stop and the statements after the exception will not be executed.

output again

cs code

So the "finally" block execution stops at the point where the exception is thrown.

Step 5: Now the third point.

third point

Let's understand with an example.

Now we throw an exception in the try block and in the finally block by the following code.
  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Text;    
  5.     
  6. namespace ExceptioninFinallyblock    
  7. {    
  8.     class Program    
  9.     {    
  10.         static void Main(string[] args)    
  11.         {    
  12.             try    
  13.             {    
  14.                 FinallyBlock();//calling method    
  15.             }    
  16.             catch(Exception ex)    
  17.             {    
  18.                 Console.WriteLine(ex.Message);    
  19.             }    
  20.         }    
  21.     
  22.         public static void FinallyBlock()    
  23.        {    
  24.            try    
  25.            {    
  26.                throw new Exception("Try Block Exception");    
  27.     
  28.            }    
  29.            finally    
  30.            {    
  31.                throw new Exception("Finally Block Exception");    
  32.              
  33.            }    
  34.        }    
  35.     }    
  36. }  
Now run the program so we are able to handle the exception that are occurs in the finally block. But what about the exception that occurs in the try block?

finallyex
So we know that whether the exception occurs or not, the finally will execute, it is guaranteed to execute.

Now this exception will occur in the try block and we are not handling the exception within this method.

And there is another exception occurring within the finally block, so as a result the original exception that occurred in the try block is lost. So the third point it is.