这篇文章对try-catch-finally语句块和return语句配合使用的所有情形应用排列组合的方式进行了总结,其中可以正常使用的十种情形以下划线标出,不考虑包含throw语句的情形。
try { return; } catch() { } 编译报错 This method must return a result
try { } catch() { return; } 编译报错 This method must return a result
try { } catch() { } return; 执行return
try { return; } catch() { return; } 无异常时执行try return,有异常时执行catch return
try { return; } catch() { } return; 无异常时执行try return,有异常时执行return
try { } catch() { return; } return; 无异常时执行return,有异常时执行catch return
try { return; } catch() { return; } return; 编译报错 Unreachable Code
try { return; } finally { } 无异常时执行try return,有异常时抛出异常
try { } finally { return; } 执行finally return,编译提示 finally block does not complete normally
try { } finally { } return; 无异常时执行return,有异常时抛出异常
try { return; } finally { return; } 无异常时计算try return,计算并执行finally return,有异常时执行finally return,编译提示 finally block does not complete normally
try { return; } finally { } return; 编译报错 Unreachable Code
try { } finally { return; } return; 编译报错 Unreachable Code
try { return; } finally { return; } return; 编译报错 Unreachable Code,编译提示 finally block does not complete normally
try { return; } catch() { } finally { } 编译报错 This method must return a result
try { } catch() { return; } finally { } 编译报错 This method must return a result
try { } catch() { } finally { return; } 执行finally return,编译提示 finally block does not complete normally
try { } catch() { } finally { } return; 执行return
try { return; } catch() { return; } finally { } 无异常时执行try return,有异常时执行catch return
try { return; } catch() { } finally { return; } 无异常时计算try return,计算并执行finally return,有异常时执行finally return,编译提示 finally block does not complete normally
try { return; } catch() { } finally { } return; 无异常时执行try return,有异常时执行return
try { } catch() { return; } finally { return; } 无异常时执行finally return,有异常时计算catch return,执行finally return,编译提示 finally block does not complete normally
try { } catch() { return; } finally { } return; 无异常时执行return,有异常时执行catch return
try { } catch() { } finally { return; } return; 编译报错 Unreachable Code,编译提示 finally block does not complete normally
try { return; } catch() { return; } finally { return; } 无异常时计算try return,计算并执行finally return,有异常时计算catch return,计算并执行finally return,编译提示 finally block does not complete normally
try { return; } catch() { return; } finally { } return; 编译报错 Unreachable Code
try { return; } catch() { } finally { return; } return; 编译报错 Unreachable Code,编译提示 finally block does not complete normally
try { } catch() { return; } finally { return; } return; 编译报错 Unreachable Code,编译提示 finally block does not complete normally
try { return; } catch() { return; } finally { return; } return; 编译报错 Unreachable Code,编译提示 finally block does not complete normally
try { return; } catch() { } 编译报错 This method must return a result
try { } catch() { return; } 编译报错 This method must return a result
try { } catch() { } return; 执行return
try { return; } catch() { return; } 无异常时执行try return,有异常时执行catch return
try { return; } catch() { } return; 无异常时执行try return,有异常时执行return
try { } catch() { return; } return; 无异常时执行return,有异常时执行catch return
try { return; } catch() { return; } return; 编译报错 Unreachable Code
try { return; } finally { } 无异常时执行try return,有异常时抛出异常
try { } finally { return; } 执行finally return,编译提示 finally block does not complete normally
try { } finally { } return; 无异常时执行return,有异常时抛出异常
try { return; } finally { return; } 无异常时计算try return,计算并执行finally return,有异常时执行finally return,编译提示 finally block does not complete normally
try { return; } finally { } return; 编译报错 Unreachable Code
try { } finally { return; } return; 编译报错 Unreachable Code
try { return; } finally { return; } return; 编译报错 Unreachable Code,编译提示 finally block does not complete normally
try { return; } catch() { } finally { } 编译报错 This method must return a result
try { } catch() { return; } finally { } 编译报错 This method must return a result
try { } catch() { } finally { return; } 执行finally return,编译提示 finally block does not complete normally
try { } catch() { } finally { } return; 执行return
try { return; } catch() { return; } finally { } 无异常时执行try return,有异常时执行catch return
try { return; } catch() { } finally { return; } 无异常时计算try return,计算并执行finally return,有异常时执行finally return,编译提示 finally block does not complete normally
try { return; } catch() { } finally { } return; 无异常时执行try return,有异常时执行return
try { } catch() { return; } finally { return; } 无异常时执行finally return,有异常时计算catch return,执行finally return,编译提示 finally block does not complete normally
try { } catch() { return; } finally { } return; 无异常时执行return,有异常时执行catch return
try { } catch() { } finally { return; } return; 编译报错 Unreachable Code,编译提示 finally block does not complete normally
try { return; } catch() { return; } finally { return; } 无异常时计算try return,计算并执行finally return,有异常时计算catch return,计算并执行finally return,编译提示 finally block does not complete normally
try { return; } catch() { return; } finally { } return; 编译报错 Unreachable Code
try { return; } catch() { } finally { return; } return; 编译报错 Unreachable Code,编译提示 finally block does not complete normally
try { } catch() { return; } finally { return; } return; 编译报错 Unreachable Code,编译提示 finally block does not complete normally
try { return; } catch() { return; } finally { return; } return; 编译报错 Unreachable Code,编译提示 finally block does not complete normally