When to choose checked and unchecked exceptions

前端 未结 18 2263
南方客
南方客 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 04:49

    Checked Exception: If client can recover from an exception and would like to continue, use checked exception.

    Unchecked Exception: If a client can't do any thing after the exception, then raise unchecked exception.

    Example: If you are expected to do arithmetic operation in a method A() and based on the output from A(), you have to another operation. If the output is null from method A() which you are not expecting during the run time, then you are expected to throw Null pointer Exception which is Run time exception.

    Refer here

提交回复
热议问题