站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2SE 专题一个关于抛出异常的程序代码的分析

一个关于抛出异常的程序代码的分析

摘要: 一个关于抛出异常的程序代码的分析
<table cellSpacing=0 cellPadding=0 border=0 class="zh114" align="right"> <tr> <td > </td> </tr> </table>
  1. public class test {
  2. public static string output = “”
  3.
  4. public static void foo(int i) {
  5. try {
  6. if(i= =1) {
  7. throw new Exception ();
  8. }
  9. output += “1”;
  10. )
  11. catch(Exception e) {
  12. output += “2”;
  13. return;
  14. )
  15. finally (
  16. output += “3”;
  17. )
  18. output += “4”;
  19. )
  20.
  21. public static void main (string args[]) (
  22. foo(0);
  23. foo(1);
  24.
  25. )
  26. )
  What is the value of the variable output at line 24?
  Ans: 13423
  答案怎么会输出5个数字呢?
  首先,foo(0),就直接到第9行。output=1
  然后,因为没有抛出异常,所以直接运行16行。
  然后运行18行。此时,output=134.
  foo(1),因为抛出异常,所以运行12行。
  然后因为catch了异常,然后运行finally里的语句。
  但是18行的不再被运行,因为13行已经要求return了。

<table width="96%"> <tr> <td background="http:///images/dian.gif" height="3"></td> </tr> </table>

↑返回目录
前一篇: 无需 JCE 用底层 API 实现开发 RSA
后一篇: 深入研究JAVA字节码反编译的方法