tags:

views:

575

answers:

10

My VS just told me;

Warning 2 CA1031 : Microsoft.Design : Modify 'Program.Main(string[])' to catch a more specific exception than 'Exception' or rethrow the exception.

Why should I do that? If I do so, and don't catch all exceptions to handle them, my program crashes with the all-popular report-screen. I don't want my users to get such error-crap!

Why should I not catch all exceptions at once to display a nice warning to the user saying: "Something went wrong, don't care about it, I will handle it, just be patient"?

Edit: Just saw I have a dupe here, sorry for that Dupe

Edit2: To clarify things; I do exit the program after any exception has been catched! I just don't want my user to see that "report to microsoft" dialog that show up when an unhandled exception is raised in a console-application!

+4  A: 

Simple answer: you are supposed to fix your bug. Find the place that throws the exception and unless it is beyond your control - fix it. Also catching (without rethrowing) all kinds of exception violates exception neutrality. In general you do not want to do this (although catching exceptions in main does look like special case)

BostonLogan
Of course I do fix my bugs, but that's not my user's concern. I log all catched exceptions in a file I can access, getting every stack-trace and whatsoever. I just don't want my user to see any error-messages.
ApoY2k
Apo Y2k: question is about best practices. As an author you are free to do whatever you want of course, but then why asking question in first place? In the bug free program user will not see "error-messages" as exception whet it thrown is caught and dealt with in appropriate place.
BostonLogan
Sure, I got the point. I was just so used to catch anything as I did with PHP all the years and never had a problem with it. Real programing is still quite a new world to me. Thanks for your advice :)
ApoY2k
A: 

When you catch general exceptions, you get the side effect of potentially hiding run-time problems from the user which, in turn, can complicate debugging. Also, by catching general exception, you're ignoring a problem (which you're probably throwing elsewhere).

JasCav
This leads to duplication of the catch-clause, especially in cases where I know that different exceptions are possible, but the effect for me is always the same - Retry or cancel?
dwo
+7  A: 

You should handle the exact exceptions you are capable of handling and let all others bubble up. If it displays a message to the user that means you don't quite know what you can handle.

Otávio Décio
That last sentence made me think... thanks
ApoY2k
If his program is main, isn't there no where else to bubble up to?
Kathy Van Stone
@Kathy - correct. In this case the user will see the undesired dialog box. The thing is, the program should identify the things it can actually handle and do so, but there are others that may leave the program in a unstable state and it should be shut down immediately.
Otávio Décio
A: 

You can set up your try catch to catch multiple different behavior types and handle the exception based on the type. For most methods and properties in the framework, you can also see what exceptions they are capable of throwing. So unless you are catching an exception from an extremely small block of code, you should probably catch specific exceptions.

marr75
+5  A: 

Because programs that swallow (catch) exceptions indiscriminately, (and then continue), cannot be relied upon to do what it is they are expected to do. This is because you have no idea what kind of exception was "ignored". What if there was an overflow or memory access error that causes the wrong amount to be debited from a financial account? What if it steers the ship into the iceberg instead of away from it ? Unexpected failures should always cause the application to terminate. That forces the development process to identify and correct the exceptions it finds, (crashes during demos are a wonderful motivator), and, in production, allows appropriately designed backup systems to react when the software experiences an "unexpected" inability to do what it was designed to do.

EDIT: To clarify distinctions between UI components, and service or middleware componentrs.

In Service or Middleware components, where there is no user interacting with the code component from within the same process space that the code is running in, the component needs to "pass On" the exception to whatever client component imnitiated the call it is currently processing. No matter the exception, it should make every possible attempt to do this. It is still the case, however, tjhat in cases where an unexpected, or unanticipated exception occurs, the component should finally terminate the process it is running in. For anticipated or expected exceptions, a velopment analysis should be done to determine whether or not, for that specific exception, the component and it's host process can continue to operate (handling future requests), or whether it should be terminated.

Charles Bretana
I actually had an instructor who insisted that you should catch all exceptions and I couldn't convince him that it was a bad idea. Needless to say, I don't do that in my normal programming.
R. Bemrose
"Unexpected failures should always cause the application to terminate." This might be ok in interactive applications, but there are many situations where throwing up your hands and exiting is inappropriate. You imply that with "appropriately designed backup systems" but it was said without qualification and with *always*. You might want to qualify/clarify.
NVRAM
@NVRAM, yes you're right in that the statement I made implies a volume of detail, but there's only so much you can put into an answer here. Nevertheless, at the risk of being absolute, I'd kinda tend (sounds tentative I guess) to stick to my "always" characterization. If an app cannot do what is expected of it, (That's my definition of an exception) then - Either you can handle the exception, (Then do so) or the user can handle it (Then ask him/her) or the application should terminate.
Charles Bretana
@NVRAM, I will add more details to the answer above later...
Charles Bretana
+4  A: 

Swallowing exceptions is a dangerous practice because:

  • It can cause the user to think something succeeded when it actually failed.
  • It can put your application into states that you didn't plan for.
  • It complicates debugging, since it's much harder to find out where the failure happened when you're dealing with bizarre/broken behavior instead of a stack trace.

As you can probably imagine, some of these outcomes can be extremely catastrophic, so doing this right is an important habbit.

Best Practice

First off, code defensively so that exceptions don't occur any more than necessary. They're computationally expensive.

Handle the expected exceptions at a granular level (for example: FileNotFoundException) when possible.

For unexpected exceptions, you can do one of two things:

  • Let them bubble up normally and cause a crash
  • Catch them and fail gracefully

Fail Gracefully?

Let's say you're working in ASP.Net and you don't want to show the yellow screen of deathto your users, but you also don't want problems to be hidden from the dev team.

In our applications here at Inntec, we usually catch unhandled exceptions in global.asax and then do logging and send out notification emails. We also show a more friendly error page, which can be configured in web.config using the customErrors tag.

That's our last line of defense, and if we end up getting an email we jump on it right away.

That type of pattern is not the same as just swallowing exceptions, where you have an empty Catch block that only exists to "pretend" that the exception did not occur.

Other Notes

In VS2010, there's something called intellitrace coming that will allow you to actually email the application state back home and step through code, examine variable values at the time of the exception, and so on. That's going to be extremely useful.

Brian MacKay
Not exactly, but good point though. I work in console based applications and I hate it like hell if a program crashes - so I caught all exceptions and printed them out (with stacktrace) in the console whilst logging them in the background.
ApoY2k
Gotcha. Well, it's still a similiar situation -- you could display a friendly message to the user, do some logging/reporting, and exit gracefully (or restart/return to a safe state).
Brian MacKay
See the answer of James Sheck, that's exactly what I did - didn't point that out though in my question...
ApoY2k
Sounds like you're all over it then! :)
Brian MacKay
+2  A: 

There is a way to suppress certain messages from code analysis. I've used this for this exact reason (catching the general exception for logging purposes) and it's been pretty handy. When you add this attribute, it shows you've at least acknowledged that you are breaking the rule for a specific reason. You also still get your warning for catch blocks that are incorrect (catching the general exception for purposes other than logging).

MSDN SuppressMessageAttribute

Austin Salonen
+3  A: 

Since your warning message shows that this is in Main(), I'll assume that in lower levels, you do catch only more specific Exceptions.

For Main(), I'd consider two cases:

  1. Your own (debugging) build, where you want all the exception information you can get: Do not catch any Exceptions here, so the debugger breaks and you have your call stack,
  2. Your public releases, where you want the application to behave normally: Catch Exception and display a nice message. This is always better (for the average user) than the 'send report' window.

To do this nicely, just check if DEBUG is defined (and define it, if VS doesn't do this automatically):

#if DEBUG
  yadda(); // Check only specific Exception types here
#else
  try
  {
    yadda();
  }
  catch (Exception e)
  {
     ShowMessage(e); // Show friendly message to user
  }
#endif

I'd disable the warning about catching general Exceptions, but only for your Main() function, catching Exception in any other method is unwise, as other posters have said already.

Lennaert
That's a very nice idea, I always liked to split up development and productive parts of a project. Wonder why I didn't think of that...
ApoY2k
Lennaert: Would it not be better to put the conditional directives specifically around the " try {" and "} catch {... }" blocks, to avoid duplicating code? (And introducing bugs, when one yadda() is changed, but not the other?)
Ian Clelland
@Ian: That's what I do normally, but I thought that this would illustrate the idea a bit better. Ideally, there would be multiple catch blocks already, with only the last block (catch Exception) in the #if/#endif.
Lennaert
+3  A: 

Having worked on equipment used by emergency responders, I would rather the user see an ugly error message than to accidently swallow an exception that misleads the user into believing everything is "ok". Depending on your application, the consequence could be anything from nothing to a lost sale to a catastrophic loss of life.

If a person were going to catch all exception, show a better error dialog, and then quit the application, that's ok.. but if they are going to continue running after swallowing an unknown exception, I would fire a person for that. It's not ok. Ever.

Good coding is about practices that assume humans make mistakes. Assuming all "critical" exceptions have been caught and handled is a bad idea.

James Schek
No, the plan was to show a nice error message and quit the program in critical state (`Environment.Exit(2)`)
ApoY2k
A: 

In VS you can setup a custom error page to show your users when something goes wrong instead of catching it in a try-catch. I'm assuming since you're using VS that you're using ASP .NET. If so add this tag to your Web.Config under the System.Web tag:

<customErrors mode="RemoteOnly" defaultRedirect="~/CustomErrorPage.aspx" redirectMode="ResponseRewrite" />

You can also catch all uncaught exceptions in the Global.asax file (if you don't have it already: Right-click on web project, select Add Item, and search for it). There are a bunch of application wide event handlers in that file like "Application_Error" that catches every exception that isn't caught within your application so you don't have to use Try-Catch all the time. This is good to use to send yourself an email if an exception occurs and possibly redirect them to your homepage or something if you don't want to use the customErrors tag above.

But ultimately you don't want to wrap your entire application in a try-catch nor do you want to catch a general Exception. Try-catches generally slow down your application and a lot of times if you catch every general exception than it could be possible that you wouldn't know a bug exists until months or years later because the try-catch caused you to overlook it.

Adam