Java 中嵌套 try catch 与常规 try catch [duplicate] 情况下的异常处理

Exception handling in Java in case of nested try catch vs regular try catch [duplicate]

提问人:rickygrimes 提问时间:11/16/2023 更新时间:11/16/2023 访问量:29

问:

我试图了解异常处理在嵌套的 try catch 与常规 try/catch 中是如何工作的。虽然这看起来很基本,但我发现理解起来相当棘手。请有人解释一下吗?

    Scenario 1
        try {
          try {
            //do something
           }catch(Exception ex) {
            throw new IOException("IOException occured");
          }
        } catch (Exception ex) {
        if(ex instanceOf IOException) {
            //do something - My assumption is the exception being thrown in inner catch is being caught here
        }
    }
    
    
   Scenario 2
    try {
        //do something
        throw new IOException;
    } catch(Exception ex) {
        if(ex instanceOf IOException) {
            //do something - will this get caught here?
        }
    
    }
    
Scenario 3
    try {
        //do something
    } catch(IOException ex) {
    
      } catch(IOException ex) {
        throw ex; // will this get caught in the calling method's catch block?
      }
    
    }
Java 异常

评论


答: 暂无答案