Guidelines on Exception propagation (in Java)

前端 未结 2 1359
傲寒
傲寒 2020-11-29 17:36

Are there any guidelines on exception propagation in Java?

When do you add an exception to the method signature? For example: if an exception is only thrown when an

2条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-29 17:58

    You should handle the method as soon as possible, but it must make sense. If the exception doesn't make sense to be thrown by your method, but you can't handle it, wrap it into another exception and throw this new exception.

    A bad practice about exception is to catch them all (it's not pokemon, it's java !) so avoid catch(Exception e) or worse catch(Throwable t).

提交回复
热议问题