views:

31

answers:

2

Hi,

I've installed visual studio 2010.

When unhandled exception is thrown, nothing happen...

I created new windows form application and wrote 1 line in the form_load function:

    private void Form1_Load(object sender, EventArgs e)
    {
        throw new Exception("");
    }

And still nothing happen. The only thing I can see is the "A first chance exception of type 'System.Exception' occurred in WindowsFormsApplication1.exe" in the output window.

It looks like this error was catch but I dont know how... (This line is the only line I wrote in this project).

How can I solve this issue?

Thanks!

A: 

Are you referring to the behaviour where the debugger breaks on the exception? If so, this is configurable and off by default. If memory serves: Debug -> Exceptions... -> Common Language Runtime Exceptions, check "Thrown".

Adam
I know this place, but this exception is not catched but anyone, so it should show me "Unhandled excpetion" error and not "first chance exception".
Mattan
A: 

You could also register your program for the UnhandledExceptionEvent. To do so write the following into your Program.cs before calling Application.Run(new MyForm()):
AppDomain.CurrentDomain.UnhandledException += OnCurrentDomainUnhandledException;

Then declare your event catcher like private static void OnCurrentDomainUnhandledException(object sender, UnhandledExceptionEventArgs e) and do what ever you want to do with that exception.

Simon Linder
Maybe I didn't explain the problem.I want that when there is unhandled exception the app will crash (As it should be), or if i'm in debug mode, it will break and show me the exception details.Now, when an unhandled exception is thrown, nothing happen...
Mattan
Then Adam's answer should do...
Simon Linder
Adam's answer refers to handled exceptions.When unhandled exception is thrown, it has to show me the error message.Or maybe in .net4 something catches the exceptions?
Mattan
I just created a new WinForms test project with the same code you have and to be honest I cannot reproduce your behavior. When I start the application the debugger stops and a MessageBox is show with "An unhandled exception of type 'System.Exception' occured..." in it. I think this is what you want to have. I just googled around and found: http://blogs.msdn.com/b/debugger/archive/2010/05/12/visual-studio-debugger-fails-to-catch-unhandled-exception-for-a-windows-form-or-wpf-application.aspx You may want to check this out.
Simon Linder
No, my answer is when any exception is throw or detected, it breaks at the source. Unhandled exceptions will be caught and displayed by the runtime. A "handled exception" is one where the code catches it and corrects the behaviour caused.
Adam
The article that Simon found describes my problem.I can see that there is nothing good to do...Thank a lot!
Mattan