Java try和catch的使用

尽管由Java运行时系统提供的默认异常处理程序对于调试是很有用的,但通常你希望自己处理异常。这样做有两个好处。第一,它允许你修正错误。第二,它防止程序自动终止。

package com.lyf;

public class My {

  public static void main(String []arg){
      int a,b;
      try{
          b=0;
          a=3/b;
          System.out.println("NO printed!");
      }catch(ArithmeticException e){
          System.out.println("Exception: "+e);
          System.out.println("Division by zero");

      }
      System.out.println("the last!");
  }
}

注意在try块中的对println( )的调用是永远不会执行的。一旦异常被引发,程序控制由try块转到catch块。执行永远不会从catch块“返回”到try块。一旦执行了catch语句,程序控制从整个try/catch机制的下面一行继续。

一个try和它的catch语句形成了一个单元。catch子句的范围限制于try语句前面所定义的语句。一个catch语句不能捕获另一个try声明所引发的异常(除非是嵌套的try语句情况)。

被try保护的语句声明必须在一个大括号之内(也就是说,它们必须在一个块中)。你不能单独使用try。
Throwable重载toString( )方法(由Object定义),所以它返回一个包含异常描述的字符串。你可以通过在println( )中传给异常一个参数来显示该异常的描述。例如,前面程序的

catch (ArithmeticException e) {
    System.out.println("Exception: " + e);
    }

被零除错误显示下面的消息:
Exception: java.lang.ArithmeticException: / by zero

try、catch、finally中的细节分析

package com.lyf;

public class My{

    public static final String test() {
        String t = "";

        try {
            t = "try";
            return t;
        } catch (Exception e) {
            // result = "catch";
            t = "catch";
            return t;
        } finally {
            t = "finally";
        }
    }

    public static void main(String[] args) {
        System.out.print(My.test());
    }

}

首先程序执行try语句块,把变量t赋值为try,由于没有发现异常,接下来执行finally语句块,把变量t赋值为finally,然后return t,则t的值是finally,最后t的值就是finally,程序结果应该显示finally,但是实际结果为try。

package com.lyf;
public class My{
    public static final String test() {
        String t = "";

        try {
            t = "try";
            return t;
        } catch (Exception e) {
            // result = "catch";
            t = "catch";
            return t;
        } finally {
            t = "finally";
            return t;
        }
    }

    public static void main(String[] args) {
        System.out.print(My.test());
    }

}

这里稍微修改了 第一段代码,只是在finally语句块里面加入了 一个 return t 的表达式。

按照第一段代码的解释,先进行try{}语句,然后在return之前把当前的t的值try保存到一个变量t’,然后执行finally语句块,修改了变量t的值,在返回变量t。

package com.lyf;

public class My{

    public static final String test() {
        String t = "";

        try {
            t = "try";
            Integer.parseInt(null);
            return t;
        } catch (Exception e) {
         System.out.println("Exception: "+e);
            t = "catch";
            return t;
        } finally {
            t = "finally";
            // System.out.println(t);
            // return t;
        }
    }

    public static void main(String[] args) {
        System.out.print(My.test());
    }
}

这里面try语句里面会抛出 java.lang.NumberFormatException,所以程序会先执行catch语句中的逻辑,t赋值为catch,在执行return之前,会把返回值保存到一个临时变量里面t ‘,执行finally的逻辑,t赋值为finally,但是返回值和t’,所以变量t的值和返回值已经没有关系了,返回的是catch

package com.lyf;

public class My{

    public static final String test() {
        String t = "";

        try {
            t = "try";
            Integer.parseInt(null);
            return t;
        } catch (Exception e) {
            System.out.println("Exception: "+e);
            t = "catch";
            return t;
        } finally {
            t = "finally";
            // System.out.println(t);
             return t;
        }
    }

    public static void main(String[] args) {
        System.out.print(My.test());
    }

}

由于try语句里面抛出异常,程序转入catch语句块,catch语句在执行return语句之前执行finally,而finally语句有return,则直接执行finally的语句值,返回finally

package com.lyf;

public class My{

    public static final String test() {
        String t = "";

        try {
            t = "try";
            Integer.parseInt(null);
            return t;
        } catch (Exception e) {
            System.out.println("Exception: "+e);
            t = "catch";
            Integer.parseInt(null);
            return t;
        } finally {
            t = "finally";
            // System.out.println(t);
             //return t;
        }
    }

    public static void main(String[] args) {
        System.out.print(My.test());
    }
}

这个例子在catch语句块添加了Integer.parser(null)语句,强制抛出了一个异常。然后finally语句块里面没有return语句。继续分析一下,由于try语句抛出异常,程序进入catch语句块,catch语句块又抛出一个异常,说明catch语句要退出,则执行finally语句块,对t进行赋值。然后catch语句块里面抛出异常。结果是抛出java.lang.NumberFormatException异常

package com.lyf;

public class My{

    public static final String test() {
        String t = "";

        try {
            t = "try";
            Integer.parseInt(null);
            return t;
        } catch (Exception e) {
            System.out.println("Exception: "+e);
            t = "catch";
            Integer.parseInt(null);
            return t;
        } finally {
            t = "finally";
            // System.out.println(t);
             return t;
        }
    }

    public static void main(String[] args) {
        System.out.print(My.test());
    }

}

这个例子和上面例子中唯一不同的是,这个例子里面finally 语句里面有return语句块。try catch中运行的逻辑和上面例子一样,当catch语句块里面抛出异常之后,进入finally语句快,然后返回t。则程序忽略catch语句块里面抛出的异常信息,直接返回t对应的值 也就是finally。方法不会抛出异常

package com.lyf;

public class My{

    public static final String test() {
        String t = "";

        try {
            t = "try";
            Integer.parseInt(null);
            return t;
        } catch (NullPointerException e) {
            t = "catch";
            return t;
        } finally {
            t = "finally";
        }
    }

    public static void main(String[] args) {
        System.out.print(My.test());
    }
}

这个例子里面catch语句里面catch的是NPE异常,而不是java.lang.NumberFormatException异常,所以不会进入catch语句块,直接进入finally语句块,finally对t赋值之后,由try语句抛出java.lang.NumberFormatException异常。

package com.lyf;

public class My{

    public static final String test() {
        String t = "";

        try {
            t = "try";
            Integer.parseInt(null);
            return t;
        } catch (NullPointerException e) {
            t = "catch";
            return t;
        } finally {
            t = "finally";
            return t;
        }
    }

    public static void main(String[] args) {
        System.out.print(My.test());
    }

}

和上面的例子中try catch的逻辑相同,try语句执行完成执行finally语句,finally赋值t 并且返回t ,最后程序结果返回finally

package com.lyf;

public class My{

    public static final String test() {
        String t = "";

        try {
            t = "try";
            Integer.parseInt(null);
            return t;
        } catch (NullPointerException e) {
            t = "catch";
            return t;
        } finally {
            t = "finally";
            String.valueOf(null);
            return t;
        }
    }

    public static void main(String[] args) {
        System.out.print(My.test());
    }
}

这个例子中,对finally语句中添加了String.valueOf(null), 强制抛出NPE异常。首先程序执行try语句,在返回执行,执行finally语句块,finally语句抛出NPE异常,整个结果返回NPE异常。

package com.lyf;

public class My{

    public static final String test() {
        String t = "";

        try {
            t = "try";
            Integer.parseInt(null);
            return t;
        } catch (Exception e) {
            t = "catch";
            return t;
        } finally {
            System.out.println("ddddddd");
        }
    }

    public static void main(String[] args) {
        System.out.print(My.test());
    }

}

结果:
ddddddd
catch
return 的结果是在所有合法程序执行之后出现的

注意:主函数调用子函数得到结果的过程,好比主函数准备一个空罐子,当子函数要返回结果时,先把结果放到罐子里,然后再将程序逻辑返回到主函数。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值