Alex Rivera | Logout

Why are try-catch in main() bad?

Asked 2009-05-18T15:12:50.077
20

Could someone explain to me why it is considered inapropriate to have a try-catch in the main() method to catch any unhandled exceptions?

[STAThread]
static void Main()
{
    try
    {
        Application.Run(new Form1());
    }
    catch (Exception e)
    {
         MessageBox.Show("General error: " + e.ToString());
    }
}

I have the understanding that this is bad practice, but not sure why.

Edit
Report

2 Answers

5

I don't think that's a bad practice in and of itself. I think the bad practice would be if that was the ONLY try/catch block you had in your application.

answered 2009-05-18T15:17:29.847
1

You've got the catch-all exception there which will trap everything. So if you've got any unhandled exceptions in you code you'll never see them.

On the positive side, your application will never crash!

If this is required behaviour then you'll need to have sufficient logging and reporting to let both the user and you, as developer, know what's happened and recover or exit as gracefully as possible.

answered 2009-05-18T15:14:12.880

Your Answer