Is “Dying is Awesome” preferred?

后端 未结 16 830
栀梦
栀梦 2020-12-08 11:09

Recently I attended Jeffrey Richter\'s training courses about .NET. He mentions one strategy of coding \"Dying is awesome\". That is, don\'t write \"catch (Exception ex)\" e

16条回答
  •  醉酒成梦
    2020-12-08 12:01

    The problem with having try catch around everything is that you often end up 'handling' things that you don't know how to recover from. The user gets a false impression that things are going fine, but internally you turn to swiss cheese and eventually break in really strange ways.

    On the other hand, throwing the exception up to the user isn't all that helpful when they don't have some way of reporting it too you

    • the new windows services provide that feature when you register with MS.
    • installing drwatson can also capture the data (dump files) for your users to send to you manually.

    If your providing a service library and it's run in process, if your service messes with the overall server messing up memory or settings and such, then perhaps the server will realy need to shut down when the exception is raised.

    By contract APIs will tend to provide a way to say 'can I do this' and then a way to 'do it', and many APIs like the MS file open will allow you to change between raising an exception and returning an error code.

提交回复
热议问题