首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

catch 跟 throws Exception 的区别

2012-10-25 
catch 和 throws Exception 的区别public class TestException {public static void main(String[] ars){A

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之后的语句不会被执行。

热点排行