Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I don't think its <em>necessarily</em> bad practice. There are a few caveats however...</p> <p>I believe the point of whoever called this "bad practice" was to reinforce the idea that you should be catching exceptions <em>closest to where they occur</em> (i.e. as high up the call stack as possible/appropiate). A blanket exception handler isn't typically a good idea because its drastically reduces the control flow available to you. Coarse-grained exception handling is quite importantly <em>not</em> a reasonable solution to program stability. Unfortunately, many beginner developers think that it is, and take such approaches as this blanket try-catch statement.</p> <p>Saying this, <em>if</em> you have utilised exception handling properly (in a fine-grained and task-specific manner) in the rest of your program, and handled the errors accordingly there (rather than juist displaying a generic error box), <em>then</em> a general try-catch for all exceptions in the <code>Main</code> method is probably a useful thing to have. One point to note here is that if you're <em>reproducably</em> getting bugs caught in this <code>Main</code> try-catch, then you either have a bug or something is wrong with your localised exception handling.</p> <p>The primary usage of this try-catch with <code>Main</code> would be purely to prevent your program from crashing in very unusual circumstances, and should do hardly any more than display a (vaguely) user-friendly "fatal error" message to the user, as well as possibly logging the error somewhere and/or submitting a bug report. So to conclude: this method <em>does</em> have its uses, but it must be done with great care, and not for the wrong reasons.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload