Extending Exception/RunTimeException in java?

前端 未结 5 1941
失恋的感觉
失恋的感觉 2020-12-04 11:15

I have below classes.

public class ValidationException extends RuntimeException {


}

and

public class ValidationException          


        
5条回答
  •  庸人自扰
    2020-12-04 11:50

    One case where it is common practice to throw a RuntimeException is when the user calls a method incorrectly. For example, a method can check if one of its arguments is incorrectly null. If an argument is null, the method might throw a NullPointerException, which is an unchecked exception.

    Generally speaking, do not throw a RuntimeException or create a subclass of RuntimeException simply because you don't want to be bothered with specifying the exceptions your methods can throw.

    Here's the bottom line guideline: If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception.

    for more read this.

提交回复
热议问题