读书人

catch 跟 throws Exception 的区别

发布时间: 2012-10-25 10:58:57 作者: rapoo

catch 和 throws Exception 的区别

public class TestException {public static void main(String[] ars){ArrayList<String> ar=new ArrayList();ar.add("str1");ar.add("str2");ar.add("str3"); TestException te=new TestException(); boolean b1; b1 = te.test1(ar); System.out.println(b1); boolean b2;try { b2 = te.test2(ar); System.out.println(b2); } catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}public boolean test1(ArrayList<String> ar){for(String cel:ar){try{System.out.println(cel);throw new Exception("test exception message!");}catch(Exception ex){System.out.println("exception");}finally{System.out.println("finally");}}return true;}public boolean test2(ArrayList<String> ar) throws Exception{for(String cel:ar){System.out.println(cel);throw new Exception("test exception message!");}return true;}}


运行 一下就明白了:
1.catch后,如果在catch段内没有return语句,那么将会继续按照原来的逻辑继续执行
2.throw exception,将会导致在发生exception之后的语句不会被执行。

读书人网 >编程

热点推荐