0

Is there any way to catch any unhandled exceptions on a form. I don't want to wrap hundreds of methods in hundreds of forms in a try catch block.

I do have a base form which each form inherits from.

Is there any way to catch any unhandled exceptions?

Thank you

Trevor
  • 2,186
  • 3
  • 16
  • 18
  • Can you be more specific. Cannot you write the try-catch in Form_Load of the desired form. – Rauf Nov 24 '11 at 13:34
  • take a look at here http://www.codeproject.com/KB/exception/ExceptionHandling.aspx – Glory Raj Nov 24 '11 at 13:35
  • Have a look at the following thread - http://stackoverflow.com/questions/793100/globally-catch-exceptions-in-a-wpf-application – Denys Wessels Nov 24 '11 at 13:37
  • see this link.Its help you. [http://stackoverflow.com/questions/5911257/asp-net-c-sharp-catch-all-exceptions-in-a-class] – 4b0 Nov 24 '11 at 13:37
  • 1
    It already works this way, there's a default handler for Application.ThreadException that displays a dialog to tell the user about the problem. You can replace it by subscribing your own event handler. – Hans Passant Nov 24 '11 at 13:39
  • Handle locally, not at some intermediate level. What should a catch all catch? – Jodrell Nov 24 '11 at 13:47

1 Answers1

0

I think there is no way to do it. You can do only something like that:

MyForm form = new MyForm();
try
{
    form.ShowDialog();
}
catch (Exception ex)
{
    ...
}

Or you can use pattern Proxy (http://en.wikipedia.org/wiki/Proxy_pattern) and create a class wrapper for a form that will have method ShowDialog and will have exception handling in it.

public class FormProxy<TForm> 
                      where TForm : Form
{
    private TForm _form;
    ....
}
Eugene
  • 1,515
  • 1
  • 13
  • 23
  • I want to catch any exceptions that happen on a form so that I can log the details of the object that was on the form that caused the error – Trevor Nov 24 '11 at 15:00