Is there an easy way to trap all exceptions inside all UI event callbacks instead of calling try/catch in each callback?
Here's what I tried so far:
UI Callback:
private void btnOk_Click(object sender, EventArgs e)
{
int x=0;
int i=1/x;//Exception happens here.
}
entry point:
public static void Main(string[] args)
{
try
{
Application.Run(new MyForm());
}
catch (Exception e)
{
Debug.Print(e.Message);
}
}
In DebugView, I can see the DivideByZero exception. However, MyForm is closed and inoperable. I was hoping it could report the exception and continue operation. Is there a simple way to make MyForm send the exception to Debug.Print() without having to add try/catch directly to btnOk_Click (and all other UI callbacks)?