Part 10 What happens if finally block throws an exception

Link for code samples used in the demo
http://csharp-video-tutorials.blogspot.com/2013/10/part-10-what-happens-if-finally-block.html

Link for all dot net and sql server video tutorial playlists
http://www.youtube.com/user/kudvenkat/playlists

This question can also be asked in a slightly different way
How to handle exceptions that occur in finally block

The exception propagates up, and should be handled at a higher level. If the exception is not handled at the higher level, the application crashes. The “finally” block execution stops at the point where the exception is thrown.

In the example below, notice that the “finally” block in “Hello()” method throws an exception. Hello() method is being called in the Main() method and we don’t have any exception handling mechanism in place in the Main() method. So, the application crashes with the exception.

public class Program
{
public static void Main()
{
Hello();
}

public static void Hello()
{
try
{
// Some code
}
catch
{
// Some code
}
finally
{
Console.WriteLine(“This line will be executed”);
int result = Convert.ToInt32(“TEN”);
Console.WriteLine(“This line will NOT be executed”);
}
}
}

On the other hand, if you include exception handling mechanism(try/catch) in the Main() method, then you will have the opportunity to handle the exception.
public static void Main()
{
try
{
Hello();
}
catch (Exception ex)
{
// Process and log the exception
Console.WriteLine(ex.Message);
}
}

Irrespective of whether there is an exception or not “finally” block is guaranteed to execute.
1. If the “finally” block is being executed after an exception has occurred in the try block,
2. and if that exception is not handled
3. and if the finally block throws an exception
Then the original exception that occurred in the try block is lost.

Here is an example:
public class Program
{
public static void Main()
{
try
{
Hello();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}

public static void Hello()
{
try
{
// This exception will be lost
throw new Exception(“Exception in TRY block”);
}
finally
{
throw new Exception(“Exception in FINALLY block”);
}
}
}

    .Net abstract abstract class advantage Advantages array arraylist AS asp.net benefits block C (Programming Language) C# c# class default constructor access modifier c# interview questions and answers c# reflection constructor access modifier c# reflection constructor not found c# reflection default constructor c# reflection get constructor c# reflection private constructor c# reflection protected constructor call cast catch class constructor csharp Data DataTypes difference between Difference between int and Int32 in c# different directory dot net dotnet Each example exception exceptions factorial files Finally folder frequently asked function generic Handle happens int vs int32 interfaces interview is jagged keyword LINQ list Lists method Number occur Operator Overriding purpose question questions Real Real-time real-world realtime Recursion recursive reverse Sentence single store String Structure subdirectories Throw throws time try Types Use using virtual method what when where Why Word World