//看一下下面的程序,你能正确的写出不同的testEx2()方法时,程序的最终打印出来的数据吗....先不要看下面的答案
public class ExceptionTest {
public ExceptionTest() {
} boolean testEx() throws Exception {
boolean ret = true;
try {
ret = testEx1();
} catch (Exception e) {
System.out.println("testEx, catch exception");
ret = false;
throw e;
} finally {
System.out.println("testEx, finally; return value=" + ret);
return ret;
}
} boolean testEx1(){
boolean ret = true;
try {
ret = testEx2();
if(!ret){
return false;
}
System.out.println("testEx1, at the end of try");
return ret; } catch (Exception e) {
System.out.println("testEx1, catch exception");
ret = false;
throw e;
} finally {
System.out.println("testEx1, finally; return value=" + ret);
return ret;
}
}
//第一种:
/* boolean testEx2() throws Exception{
boolean ret = true;
try { int b = 12;
int c;
for (int i = 2; i >= -2; i--) {
c = b / i;
System.out.println("i=" + i);
}
return ret;
} catch (Exception e) {
System.out.println("testEx2, catch exception");
ret = false;
throw e;
} finally {
System.out.println("testEx2, finally; return value=" + ret);
return ret;
}
} */ //第二种:
boolean testEx2() throws Exception {
boolean ret = true;
int b = 12;
int c;
for (int i = 2; i >= -2; i--) {
c = b / i;
System.out.println("i=" + i);
}
System.out.printf("这句话打打出来了吗??????");
return true;
} //第三种:
/*boolean testEx2() throws Exception{
boolean ret = true;
try { int b = 12;
int c;
for (int i = 2; i >= -2; i--) {
c = b / i;
System.out.println("i=" + i);
}
return ret;
} catch (Exception e) {
System.out.println("testEx2, catch exception");
ret = false;
throw e;
} finally {
System.out.println("testEx2, finally; return value=" + ret);
//return ret; //此处不写return 语句
}
//System.out.println("fhsdfsdofi");//因为try中有一个直接return语句,所以这两句不能被访问
//return ret;
} */ //第四种: /*boolean testEx2() throws Exception{
boolean ret = true;
try { int b = 12;
int c;
for (int i = 2; i >= -2; i--) {
c = b / i;
System.out.println("i=" + i);
}
} catch (Exception e) {
System.out.println("testEx2, catch exception");
//ret = false;
throw e;
} finally {
System.out.println("testEx2, finally; return value=" + ret);
//return ret; //此处不写return 语句
}
System.out.println("这句话打印出来了!!!!");
return ret;
} */
//第五种: /*boolean testEx2() throws Exception{ //提醒一下,第四种和第五种只有catch中有没有 throw e 不一样
boolean ret = true;
try { int b = 12;
int c;
for (int i = 2; i >= -2; i--) {
c = b / i;
System.out.println("i=" + i);
}
//return ret;
} catch (Exception e) {
System.out.println("testEx2, catch exception");
ret = false;
//throw e;
} finally {
System.out.println("testEx2, finally; return value=" + ret);
//return ret; //此处不写return 语句
}
System.out.println("这句话打印出来了!!!!!!!");
return ret;
}*/ public static void main(String[] args) {
ExceptionTest testException1 = new ExceptionTest();
try {
testException1.testEx();
} catch (Exception e) {
e.printStackTrace();
}
}
} class myException extends Exception{
public void printException(){
System.out.println("产生异常!");
}
} /*异常看着容易,理解起来也很容易!但是它真的没有你想像的那么简单!
第一种:
i=2
i=1
testEx2, catch exception
testEx2, finally; return value=false
testEx1, finally; return value=false
testEx, finally; return value=false 第二种:
i=2
i=1
testEx1, catch exception
testEx1, finally; return value=false
testEx, finally; return value=false 第三种:
i=2
i=1
testEx2, catch exception
testEx2, finally; return value=false
testEx1, catch exception
testEx1, finally; return value=false
testEx, finally; return value=false 第四种:
i=2
i=1
testEx2, catch exception
testEx2, finally; return value=true
testEx1, catch exception
testEx1, finally; return value=false
testEx, finally; return value=false 第五种:
i=2
i=1
testEx2, catch exception
testEx2, finally; return value=false
这句话打印出来了!!!!!!!
testEx1, finally; return value=false
testEx, finally; return value=false 总结一下:
一:throw new Exception()
第一种情形:(由第二种情形验证)
int test() throws Exception{
if(....)
throw new Exception();
....
return x;
}
第二中情形:第五种情况可验证
int test() throws Exception{
try{
if(...)
throw new Exception();
}catch(ArithmeticException e){//在try中产生的异常没有被捕获时:
....
}finally{
....
}
.....
return x;
}
在执行到throw 时,第一种先将return返回个调用者(也就是throw和return之间的语句不会被执行),然后再调用程序中寻找处理异常的程序
第二种还要将 finally中的语句执行完(即异常有没有被立即处理都要执行finally),(throw和return之间的语句也不会被执行),
然后执行return语句,程序转向调用者中寻找异常处理程序。
二:再让我们看一下finally中写return的一些缺憾
1 finally块中的return语句会覆盖try块、catch块中的return语句
2 如果finally块中包含了return语句,即使前面的catch块重新抛出了异常,则调用该方法的语句也不会获得catch块重新抛出的异常,
而是会得到finally块的返回值,并且不会捕获异常,也就是如果在catch或者try中产生的异常如果在向外界抛出是不可能的。。。。 第一种情况:testEx2()方法中会产生一个ArithmeticException的异常, Exception是它的父类,我们在该方法中捕获了该异常并进行了处理
所以会输出 testEx2()中的 catch 和 finally 中的输出数据, 而在testEx1()方法中,调用了testEx2(),由于testEx2()中的异常已经被处理
并且由于finally中的return语句导致testEx2()中catch中的throw e 无法重新抛出,所以在testEx1()中不会被捕获。 再说一下第四,第五种情况:fianlly中都没有return
在第四种情况中, 由于在catch中继续抛出了该异常,该异常在testEx2()中没有被处理,所以在执行finally之后的语句(除了return语句)不会被执行,
而第五种是没有继续抛出该异常,也就是textEx2()中产生的异常全部被处理了,所以finally之后的语句会被执行.....其他不多说。 如果还是没有搞懂的话,推荐看一下下面这个链接,写的不错....
http://blog.csdn.net/hguisu/article/details/6155636
*/

  

有关try..catch..finally处理异常的总结的更多相关文章

  1. Java如何使用catch来处理异常?

    在Java编程中,如何使用catch块来处理异常? 此示例显示如何使用catch来处理异常. package com.yiibai; public class UseOfCatch { public ...

  2. 异常处理----使用 try…catch…finally 处理异常

    使用 try…catch…finally 处理异常 异常处理是通过try-catch-finally语句实现的. try { ...... //可能产生异常的代码 } catch( Exception ...

  3. php写错命名空间 导致catch不到异常

    写的微信回调接口出错了, 由于手里的调试工具(包括微信官方的开发者接口调试工具)不能把HTTP错误的详情dump出来,只会显示空白,所以打算在程序里加上try catch 捕获错误直接输出.重新测试, ...

  4. php Try Catch多层级异常测试

    <?php class a { public function a1 () { try { throw new Exception('123'); } catch (Exception $e) ...

  5. 如何创建一个可以使用try.....catch.......捕获的异常

    代码很简单,大家一看基本上就能明白(有一定的java基础,熟悉try......catch.....finally的使用方法) package com.nokia.test1; public clas ...

  6. try~Catch语句中异常的处理过程

    [2014/10/12 21:40]文章待续~ 1.函数自身捕获处理异常的情况 以下的样例介绍了try~catch语句中出现异常时语句的运行顺序: package month10; import ja ...

  7. Java基础 try...catch(多个异常) 多个异常采取同样的解决措施

        JDK :OpenJDK-11      OS :CentOS 7.6.1810      IDE :Eclipse 2019‑03 typesetting :Markdown   code ...

  8. try/catch捕获处理异常

    1.throws是中断处理,后续代码不能执行 try/catch方法体之后的后续代码有没有异常都可以继续执行: 2.当try方法体中出现异常才会执行catch方法体中代码

  9. EF提交插入数据catch捕获具体异常方法

    try { db.SaveChanges(); } catch (DbEntityValidationException ex) { StringBuilder errors = new String ...

随机推荐

  1. thinkphp学习笔记(一)

    一.入门与安装 1.进入C:\Windows\System32\drivers\etc的hosts文件中增加127.0.0.1 www.tp5.com 即访问这个域名就相当于访问127.0.0.1 2 ...

  2. PYTHON学习之路_PYTHON基础(8)

    学习内容: Python模块介绍 1.经典类 or 新式类 2.抽象接口 3.静态方法.类方法.属性方法 4.反射 5.异常处理 6.socket编程初识 7.用socket实现get.put文件等功 ...

  3. 各廠商ERP系統架構圖連結 (ERP流程圖)(轉)

    各廠商ERP系統架構圖連結 (ERP流程圖)   資料來源 Google圖片搜尋ERP整理而來 資通電腦 ArgoERP 資通電腦 Oracle ERP 鼎新電腦 Workflow ERP鼎新電腦 S ...

  4. 2.一起来学hibernate之配置文件1与持久化对象

    学框架,配置都是不可少的,有了配置,框架才知道我们想做什么,才知道如何去执行我们需要的操作! hibernate的配置文件,总体来说分为两个部分: 1.主配置文件hibernate.cfg.xml文件 ...

  5. 【C-循环结构】

    C语言提供了多种循环语句,可以组成各种不同形式的循环结构: 用goto语句和if语句构成循环: 用while语句: 用do-while语句: 用for语句: 一.goto语句 goto语句是一种无条件 ...

  6. 03人人都应该了解的10个 jQuery 小技巧

    1  返回顶部按钮 你可以利用animate和scrollTop来实现返回顶部的动画,而不需要使用其他插件. // Back to top $('a.top').click(function () { ...

  7. Java构建工具Ant小记(一)

    Ant简介 Ant是基于java的构建工具.理论上来说它类似与make工具,但是却克服了make的一些固有的缺陷. 传统的Make是基于操作系统shell的构建工具,虽然也可以基于工作的os对make ...

  8. [转] 编译安装GCC

    Linux下编写C/C++程序自然缺不了一个优秀的编译器,Linux下比较常见的自然是GCC了. 2015年GCC也出到了5.2.0版本,对于C++11/14也有了更好的支持了. 所以,今天我们就来说 ...

  9. 创建动态WCF服务(无配置文件)

    public class WCFServer { ServiceHost host = null; public WCFServer(string addressurl, string tcpurl, ...

  10. [.NET领域驱动设计实战系列]专题一:前期准备之EF CodeFirst

    一.前言 从去年已经接触领域驱动设计(Domain-Driven Design)了,当时就想自己搭建一个DDD框架,所以当时看了很多DDD方面的书,例如领域驱动模式与实战,领域驱动设计:软件核心复杂性 ...