欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

分享一个很有意思的异常

程序员文章站 2024-03-12 20:14:50
...

有这样一段代码:

try{
    pageQueryResult = customerInfoImpl.getCustomList(customRequese,pageIndex,pageSize);
    if(pageQueryResult==null){
        Exception.throwCommonException(CustomerInfoImpl.error);
    }
}catch (Exception e){
    e.printStackTrace();
}


这里面 Exception.throwCommonException(CustomerInfoImpl.error);是自定义的弹框异常,捕捉到这个异常之后会有错误的弹框提醒,错误信息是CustomerInfoImpl.error但是实际的运行情况是执行完try之后,然后执行的是catch,然后抛出的异常就变成e.printStackTrace();显示的异常信息就变成了平台异常,没有捕捉到正确的异常信息。所以这里的try catch捕捉异常用的很莫名其妙。直接这样写不就好了 :

pageQueryResult = customerInfoImpl.getCustomList(customRequese,pageIndex,pageSize);
    if(pageQueryResult==null){
        Exception.throwCommonException(CustomerInfoImpl.error);
    }

如果捕捉到相应的异常直接有相应的具体异常提示。因为你本来就是想要CustomerInfoImpl.error的错误提示。没有必要再去用e.printStackTrace()提示。这段代码真的是让人有点感觉写的很逗比。 拿出这段代码出来分享一下,如果各位遇到这种莫名奇妙的错误,也能很快定位到错误的原因。如果各位大神对于这段代码有什么别的见解的话,也请不吝赐教。