Would you ever NOT catch an exception, or throw an exception that won't be caught?

前端 未结 14 851
感情败类
感情败类 2021-01-03 09:49

I\'ve dealt with instances where I would throw/rethrow an exception knowing that the code surrounding it would catch the specific exception. But is there any time you would

相关标签:
14条回答
  • 2021-01-03 10:04

    Sure. For example, if you're trying to load some bytes into a string in Java:

    try {
      String myString = new String(byteArray, "UTF-8");
    } catch (UnsupportedEncodingException e) {
      // Platform doesn't support UTF-8?  What is this, 1991?
      throw new RuntimeExceptione(e);
    }
    

    In this case, there is no graceful degradation, the platform simply can't support the operation desired. You can check for this condition at initialization all you want, but the constructor for String still throws this exception, and you have to deal with it. Either that, or use Charset.forName() :)

    0 讨论(0)
  • 2021-01-03 10:08

    There's a very good rule that I came across a while ago:

    Throw an exception when a method can't do what its name says it does.

    The idea is that an exception indicates that something has gone wrong. When you are implementing a method, it is not your responsibility to be aware of whether it will be used correctly or not. Whether the code using your method catches the exception or not is not your responsibility, but the responsibility of the person using your method.

    Another rule to follow is:

    Don't catch an exception unless you know what you want to do with it.

    Obviously, you should include cleanup code in a try...finally block, but you should never just catch an exception just for the sake of catching it. And you should never swallow exceptions silently. While there are occasions when you may want to catch all exceptions (e.g. by doing catch (Exception ex) in C#), these are fairly uncommon and generally have a very specific technical reason. For example, when you are using threads in .NET 2.0 or later, if an exception escapes from your thread, it will cause the entire application domain to unload. In these cases, however, at the very minimum you should log the exception details as an error and provide an explanation in the comments.

    0 讨论(0)
  • 2021-01-03 10:12

    But is there any time you would want to throw an exception, knowing that it wouldn't be caught?

    I would say that if you're manually throwing an exception, most of the time you don't know if it will be caught. If you knew it would be caught you could just handle it yourself rather than throwing the exception in the first place.

    To be fair, I suppose that depends in part on the kind of programming you're doing, and sometimes the same programmer ends up building both the library and the code that consumes said library.

    Would you ever NOT catch an exception?

    If you didn't expect/weren't aware an exception could be thrown. But putting that aside and assuming you are aware of the exception, sometimes you know about it at one tier but know the next tier up is the more appropriate place to handle it.

    0 讨论(0)
  • 2021-01-03 10:12

    If you're writing the entire application, then your reasons are your own. I can think of a few situations where you might want to throw the exception and let the app die, most of them are not very good reasons though.

    The best reason is usually when debugging. I frequently disable exceptions while debugging to allow me to know better where something is failing. You can also just turn on thrown exception breaks in the debugger if you're running it on a machine with the debugger.

    Another possible reason is when continuing after an exception is thrown doesn't make sense or would result in possible irrecoverable data corruption or worse (think Robots with laser beams, but then you should be damn sure your applicaiton deals with these situations IMO, crashing the program is just the lazy way).

    If you're writing API code, or Framework code that you won't use yourself, then you have no idea if someone will catch your exceptions.

    0 讨论(0)
  • 2021-01-03 10:15

    You probably wouldn't want an uncaught exception anywhere where the end-users can see it, but it is often acceptable to let clients of your API (other programmers) decide how to handle exceptions.

    For example, suppose you are designing a Java class library. You expose a public method that takes in a String. In your application, a null input value would cause an error. Instead of handling the error yourself, it would be acceptable to check for a null value, then throw an IllegalArgumentException.

    You must, of course, document that your method throws this exception in this circumstance. This behavior becomes part of your method's contract.

    0 讨论(0)
  • 2021-01-03 10:17

    Yup, it's my ONLY opportunity to slap the developer consuming the service/object to tell them "Ur dO1n it WrOnG!!!!".

    That and getting rid of possibilities that you don't want to permit or are seemingly "impossible". Apps that catch all exceptions and continue are just a walled garden surrounded by chaos.

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