Reputation: 2405
There are some cases where control won't pass through the finally block of a try-catch-finally statement when the .NET framework determines that exceptions thrown by the catch block are not going to be caught anywhere in the code.
Can this behavior be configured, and if so how?
(Before you tell me that control ALWAYS passes through the finally block, feel free to conduct the following experiment and report your observations.)
Upvotes: 0
Views: 86
Reputation: 2405
I think the observed behavior is related to the way the VS debugger was configured.
Upvotes: 0
Reputation: 12857
Works as expected when I rethrow the exception, code:
class Program
{
static void Main(string[] args)
{
System.Console.WriteLine("Start");
try
{
System.Console.WriteLine("Try");
throw new Exception("ttt");
}
catch (System.Exception ex)
{
System.Console.WriteLine("Catch");
throw;
}
finally
{
System.Console.WriteLine("Finally");
}
}
}
here is my output from my code:
Start
Try
Catch
Unhandled Exception: System.Exception: ttt
at ConsoleApplication1.Program.Main(String[] args) in C:\Users\mckeownto01\Documents\Visual Studio 2010\Projects\MvcApplication1\ConsoleApplication1\Program.cs:line 21
Finally
Upvotes: 2
Reputation: 112752
If you have a WinForms app, add these lines before Application.Run(new Form1());
in your Main
routine.
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
Application.ThreadException += Application_ThreadException;
And add this method to the Program
class:
static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
//TODO: Handle the error here.
}
Upvotes: 0