public Integer test(Integer v) {
if (v != null) {
return v + 1;
} else {
throw new RuntimeException("null"); // 直接这样写是没错的
// 如果把这句话封装进其他类,就会碰到一楼说的问题
//
// 被迫写成
// ErrorHandler.raise("null"); <- 里面的代码就是throw new RuntimeException
// return null; <- 这句话看起来太别扭,明明上面实际已经抛出异常,之后的语句不会被执行。但是为了消除JAVA的编译错误,不得不加了这么一句话
if (v != null) {
return v + 1;
} else {
throw new RuntimeException("null"); // 直接这样写是没错的
// 如果把这句话封装进其他类,就会碰到一楼说的问题
//
// 被迫写成
// ErrorHandler.raise("null"); <- 里面的代码就是throw new RuntimeException
// return null; <- 这句话看起来太别扭,明明上面实际已经抛出异常,之后的语句不会被执行。但是为了消除JAVA的编译错误,不得不加了这么一句话
要么返回null值,但是在调用方去检查null值并进行相应处理。
为避免NullPointerException改抛RuntimeException能得到什么好处呢?
不知道lz是怎么封装的,如果封装到方法中首先要instance of判断一下,然后处理,如果通过类来解析异常,就需要你实现你抛异常的父类?估计lz没有使用得当
pubic class TestException extends RuntimeException{
public TestException(String msg){
super(msg);
}
}