美文网首页
try-catch-finally 语句的执行问题

try-catch-finally 语句的执行问题

作者: 环球探测 | 来源:发表于2016-08-30 20:28 被阅读45次

    关于这个经典问题,oracle其实有官方文档来进行阐释。 Execution of try-finally and try-catch-finally
    本文试图对这部分文档做一个简单的修改,制成伪码形式,以加深记忆

    java
    // 翻译版
    执行 try 块;
    if(try 块的执行顺利完成){
    执行 finally 块;
    if(finally 块正常执行完成){
    try 块执行正常完成; // 所以,即使try中有return语句,也会执行finally的;
    }else if(finally 块由于原因 S 失败,结束执行){
    try 块执行失败,原因 S;
    }
    }else if (try 块的执行由于抛出异常 v 而终止){
    if(如果 v 能够与后续的 catch 语句异常类型匹配){
    选择第一个匹配的 catch 语句进行 catch 语句执行;
    if(catch 块正常执行){
    执行 finally 块;
    if(finally 块正常执行){
    try 块正常结束执行;
    else if(finally 块由于原因 S 异常终止){
    try 块由于原因 S 异常终止;
    }
    }else if(catch 块由于原因 R 异常终止){
    执行 finally 块;
    if(finally 块正常执行){
    try 语句由于原因 R 异常终止;
    }else if(finally 块由于原因 S 异常终止){
    try 语句由于原因 S 异常终止(原因 R 被忽略);
    }
    }
    }else{
    执行 finally 块;
    if(finally 块正常执行完成){
    try 语句抛出异常 v;
    }else if (finally 块由于原因 S 异常终止){
    try 语句由于原因 S 异常终止; ( 异常 v 将被无视)
    }
    }
    }else if(try 语句由于其他原因 R 异常终止){
    execute finally block;
    if(finally 块正常执行){
    try 语句由于 R 而异常终止;
    }else if(finally 块由于原因 S 异常终止){
    try 语句由于原因 S异常终止(原因 R 忽略掉);
    }
    }
    // 英语原文
    execute try block;
    if(execution of try block completes normally){
    execute finally block;
    if(finally block completes normally){
    try block completes normally;
    }else if(finally block completes abruptly for reason S){
    try statement completes abruptly for reason S;
    }
    }else if (execution of try block completes abruptly because of a throw of a value v){
    if(v is assignment compatible with a catchable exception class of any catch clause){
    the first matchable catch clause is selected;
    execute the selected catch clause with parameter v;
    if(catch block completes normally){
    execute the finally block;
    if(finally block completes normally){
    try block completes normally;
    else if(finally block completes abruptly for reason S){
    try statement completes abruptly for reason S;
    }
    }else if(catch block completes abruptly for reason R){
    execute finally block;
    if(finally block completes normally){
    try statement completes abruptly for reason R;
    }else if(finally block completes abruptly for reason S){
    try statement completes abruptly for reason S(and reason R is discarded);
    }
    }
    }else{
    execute finally block;
    if(finally block completes normally){
    try statement complete abruptly because of a throw of v;
    }else if (finally block completes abruptly for reason S){
    try statement complete abruptly because of the reason S; ( the throw of v is discard and forgotten)
    }
    }
    }else if(execution of try block completes abruptly because of any other reason R){
    execute finally block;
    if(finally block completes normally){
    try statement completes abruptly for reason R;
    }else if(finally block completes abruptly for reason S){
    try statement completes abruptly for reason S(and reason R is discarded);
    }
    }

    相关文章

      网友评论

          本文标题:try-catch-finally 语句的执行问题

          本文链接:https://www.haomeiwen.com/subject/jdqqettx.html