Catching base Exception class in .NET

前端 未结 16 1922
心在旅途
心在旅途 2020-12-05 19:06

I keep hearing that

catch (Exception ex)

Is bad practise, however, I often use it in event handlers where an operation may for example go

相关标签:
16条回答
  • 2020-12-05 19:13

    Yes, it is fine to catch the base Execption at the top level of the application, which is what you are doing.

    The strong reactions you are getting is probably because at any other level, its almost always wrong to catch the Base exception. Specifically in an a library it would be very bad practice.

    0 讨论(0)
  • 2020-12-05 19:14

    It is bad practice in the sense that you shouldn't do it everywhere.

    In this case, I would consider it the only reasonable solution as your exception could be truly anything. The only possible improvement would be to add extra handlers before your catch everything for specific error cases where you could do something about the exception.

    0 讨论(0)
  • 2020-12-05 19:14

    It's perfectly okay if you re-raise exceptions you can't handle properly. If you just catch the exceptions you could hide bugs in the code you don't expect. If you catch exceptions to display them (and bypass the die-and-print-traceback-to-stderr behavior) that's perfectly acceptable.

    0 讨论(0)
  • 2020-12-05 19:16

    a lot of times exception are catched to free resources, it' s not important if exception is (re)thrown. in these cases you can avoid try catch:

    1) for Disposable object you can use "using" keyword: using(SqlConnection conn = new SqlConnection(connStr)) { //code } once you are out of the using scope (normally or by a return statement or by exception), Dispsose method is automatically called on object. in other word, it' s like try/finally construct.

    2) in asp.net, you can intercept Error or UnLoad event of Page object to free your resource.

    i hope i help you!

    0 讨论(0)
  • 2020-12-05 19:17

    I find the arguments that generic catches are always bad to be overly dogmatic. They, like everything else, have a place.

    That place is not your library code, nor the classes you custom-develop for your app. That place is, as many have mentioned, the very top level of the app, where if any exception is raised, it is most likely unexpected.

    Here's my general rule (and like all rules, it's designed to be broken when appropriate):

    I use classes and custom-built libraries for the majority of the lifting in an app. This is basic app architecture -- really basic, mind you. These guys try to handle as many exceptions as possible, and if they really can't continue, throw the most specific kind available back up to the UI.

    At the UI, I tend to always catch all from event handlers. If there is a reasonable expectation of catching a specific exception, and I can do something about it, then I catch the specific exception and handle it gracefully. This must come before the catch all, however, as .NET will only use the very first exception handler which matches your exception. (Always order from most specific to most generic!)

    If I can't do anything about the exception other than error out (say, the database is offline), or if the exception truly is unexpected, catch all will take it, log it, and fail safe quickly, with a general error message displayed to the user before dying. (Of course, there are certain classes of errors which will almost always fail ungracefully -- OutOfMemory, StackOverflow, etc. I'm fortunate enough to have not had to deal with those in prod-level code ... so far!)

    Catch all has its place. That place is not to hide the exception, that place is not to try and recover (because if you don't know what you caught, how can you possibly recover), that place is not to prevent errors from showing to the user while allowing your app to continue executing in an unknown and bad state.

    Catch all's place is to be a last resort, a trap to ensure that if anything makes it through your well-designed and well-guarded defenses, that at a minimum it's logged appropriately and a clean exit can be made. It is bad practice if you don't have well-designed and well-guarded defenses in place at lower levels, and it is very bad practice at lower levels, but done as a last resort it is (in my mind) not only acceptable, but often the right thing to do.

    0 讨论(0)
  • 2020-12-05 19:19

    The important thing is to understand the path of exceptions through your application, and not just throw or catch them arbitrarily. For example, what if the exception you catch is Out-Of-Memory? Are you sure that your dialog box is going to display in that case? But it is certainly fine to define a last-ditch exception point and say that you never want errors to propagate past that point.

    0 讨论(0)
提交回复
热议问题