When to choose checked and unchecked exceptions

前端 未结 18 2257
南方客
南方客 2020-11-22 04:13

In Java (or any other language with checked exceptions), when creating your own exception class, how do you decide whether it should be checked or unchecked?

My inst

18条回答
  •  抹茶落季
    2020-11-22 05:13

    I think when declaring Application Exception it should be Unchecked Exception i.e., subclass of RuntimeException. The reason is it will not clutter application code with try-catch and throws declaration on method. If your application is using Java Api which throws checked exceptions that anyways need to be handle. For other cases, the application can throw unchecked exception. If the application caller still needs to handle unchecked exception, it can be done.

提交回复
热议问题