Wrapping a checked exception into an unchecked exception in Java?

后端 未结 4 647
我在风中等你
我在风中等你 2020-12-08 00:48

I have this factory method in java:

public static Properties getConfigFactory() throws ClassNotFoundException, IOException {
    if (config == null) {
               


        
4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-08 01:20

    Two points regarding exception handling best practices:

    • Caller code cannot do anything about the exception -> Make it an unchecked exception
    • Caller code will take some useful recovery action based on information in exception -> Make it a checked exception

    And you may throw the RuntimeException with or without inner exception, depending on what the caller can do with it. If you're not rethrowing the inner exception then you should log it in your method, if important.

提交回复
热议问题