Need authoritative source for why you shouldn't throw or catch java.lang.Exception

后端 未结 9 1092
无人及你
无人及你 2020-12-19 09:27

I\'ve got a coding standards meeting in just over an hour and I need a quick answer to this one.

Common wisdom among experienced Java programmers is that you don\'t

9条回答
  •  执念已碎
    2020-12-19 10:13

    Here is a very insightful article

    Summary:

    1. Use checked exceptions for rare but valid contingencies that relate to the purpose of your code. Client code should know how to handle these.

    2. Use unchecked exceptions for faults that shouldn't happen but do. (Server down, classpath misconfigured, SQL syntax error, etc.) The IT guy who manages the server, or the developer who just passed bad SQL to prepareStatement() should know how to fix these problems. Faults should propagate up to the logging layer so the info doesn't get lost.

提交回复
热议问题