且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

两次捕获相同的异常

更新时间:2023-02-05 19:08:31

如果catch块内的anotherMethodThrowingIllegalArgumentException()调用可能引发异常,则应在此处捕获该异常,而不应将其作为***" 声明:

If the anotherMethodThrowingIllegalArgumentException() call inside the catch block may throw an exception it should be caught there, not as part of the "top level" try statement:

public void method(){

    try{
        methodThrowingIllegalArgumentException();
        return;
    catch (IllegalArgumentException e) {
        try {
            anotherMethodThrowingIllegalArgumentException();            
            return;
        } catch(IllegalArgumentException eee){
            //do some
            return;
        }
    } catch (SomeAnotherException ee){
       return;
    }
}