package 異常;  
 
public class TestException {  
    public TestException() {  
    }  
 
    boolean testEx() throws Exception {  
        boolean ret = true;  
        try {  
            ret = testEx1();  
        } catch (Exception e) {  
            e.printStackTrace();
            System.out.println("testEx, catch exception");  
            ret = false;  
            throw e;  
        } finally {  
            System.out.println("testEx, finally; return value=" + ret);  
            return ret;  
        }  
    }  
 
    boolean testEx1() throws Exception {  
        boolean ret = true;  
        try {  
            ret = testEx2();  
            if (!ret) {  
                return false;  
            }  
            System.out.println("testEx1, at the end of try");  
            return ret;  
        } catch (Exception e) {  
            e.printStackTrace();
            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 true;  
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("testEx2, catch exception");  
            ret = false;  
           //極其重要 throw 可以代替 return 若後面 finally 中是 return 調用此方法的將不會收到異常
           //(即catch裏面作爲結果抛出的異常被finally裏面的return覆蓋了)   若沒有return也沒有抛出某異常  則調用的將會接到此處抛出的異常並處理
            throw e;    
        } finally {  
            System.out.println("testEx2, finally; return value=" + ret);  
//            return ret;  
            throw new ArrayIndexOutOfBoundsException();
        }
//        System.out.println("執行中");
//        return false;
    }  
 
    public static void main(String[] args) {  
        TestException testException1 = new TestException();  
        try {  
            testException1.testEx();  
        } catch (Exception e) {  
            e.printStackTrace();  
            System.out.println("main");
        }  
    }  
}

結果

i=2
i=1
java.lang.ArithmeticException: / by zero
    at 異常.TestException.testEx2(TestException.java:48)
    at 異常.TestException.testEx1(TestException.java:25)
    at 異常.TestException.testEx(TestException.java:10)
    at 異常.TestException.main(TestException.java:71)
testEx2, catch exception
testEx2, finally; return value=false
java.lang.ArrayIndexOutOfBoundsException
    at 異常.TestException.testEx2(TestException.java:62)
    at 異常.TestException.testEx1(TestException.java:25)
    at 異常.TestException.testEx(TestException.java:10)
    at 異常.TestException.main(TestException.java:71)
testEx1, catch exception
testEx1, finally; return value=false
testEx, finally; return value=false

如果所有方法都层层上抛获取的异常,最终JVM会进行处理,处理也很简单,就是打印异常消息和堆栈信息

質料   http://www.cnblogs.com/to-creat/p/5688235.html

java 異常抛出 throw 與 return的更多相关文章

  1. java中异常的抛出:throw throws

    java中异常的抛出:throw throws Java中的异常抛出 语法: public class ExceptionTest{ public void 方法名(参数列表) throws 异常列表 ...

  2. java中抛出throw关键字是怎么用的? 举例?

    5.抛出throw关键字 马克-to-win:我们先说5/0的原理,当程序运行到5/0的时候,java系统JVM会在后台new出一个除0异常实例,之后把这个实例传入catch块儿供开发者使用.马克-t ...

  3. java的异常抛出throws和throw的简单使用

    前提: 当在程序测试时,如果你需要定义一个自己的异常,而非现在已经存在的异常,这个时候你需要用到throws和throw,try-catch只是一个简单的捕获异常的过程. 代码如下: package ...

  4. 【JAVASE】Java同一时候抛出多个异常

    Java有异常抛出后.跳出程序.一般无法运行接下来的代码. 大家做登陆功能.常常会实username和password的登陆校验,username或者password错误.假设通常是提示usernam ...

  5. 【优化】自定义抛出throw 对象练习

    package ltb6w; import java.util.*; public class Bank { private boolean bool=true; private String sel ...

  6. 自定义抛出throw 对象练习

    package ltb6w; import java.util.*; public class Bank { private String select; private String select2 ...

  7. java的异常抛出和String类常用方法

    一.异常抛出 异常是程序的异种非错误的意外情况,分为运行期异常(RuntimeException)和编译期异常(CheckedExcption) 处理异常可以用try——catch或自定义 impor ...

  8. java使用json抛出org.apache.commons.lang.exception.NestableRuntimeException解决方案

    出现这个问题,说明缺少jar包,将下面的jar引入即可 commons-beanutils-1.8.3 commons-lang-2.6 (注:导入最新的 3.1 版本会继续报如下错误) common ...

  9. java-异常-原理异常对象的抛出throw

    1 class Demo { 2 public static int method(int[] arr,int index) { 3 4 // System.out.println(arr[index ...

随机推荐

  1. Mysql索引,有哪几种索引,什么时候该(不该)建索引;SQL怎么进行优化以及SQL关键字的执行顺序

    索引(Index)是帮助MySQL高效获取数据的数据结构.提取句子主干,就可以得到索引的本质:索引是数据结构. 1.按照索引列值的唯一性,索引可分为唯一索引和非唯一索引 非唯一索引:B树索引 crea ...

  2. chattr 改变文件、目录属性 (chmod、passwd等涉及文件修改的命令提示Operation not permitted)

    与chmod这个命令相比,chmod只是改变文件的读写.执行权限,更底层的属性控制是由chattr来改变的. lsattr查看文件或目录属性 chattr命令的用法:chattr [ -RVf ] [ ...

  3. javascript中函数作用域和声明提前

    javascript不像java等其他强类型语句,没有块级作用域(括号内的代码都有自己的作用域,变量在声明它们的代码段之外不可见)一说,但有自己的独特地方,即函数作用域. 函数作用域:变量在声明它们的 ...

  4. 解决linux中ssh登录Warning:Permanently added (RSA) to the list of known hosts

    原因: 在执行scp id_rsa.pub root@hostname:/root/.ssh这一步时,没在本机的/root/.ssh下生成known_hosts文件. 解决方案: vi /etc/ss ...

  5. Object-c 调用unity的 UIViewController和UnitySendMessage

    .mm文件中实现 #import <UIKit/UIKit.h> extern UIViewController *UnityGetGLViewController(); extern & ...

  6. Extjs4 自定义颜色选择器ColorField

    使用元素的颜色选择器 Ext.create("Ext.picker.Color",{ renderTo:Ext.getBody(), listeners:{ select:func ...

  7. 三种方式监听NGUI的事件方法

    NGUI研究院之三种方式监听NGUI的事件方法(七) NGUI事件的种类很多,比如点击.双击.拖动.滑动等等,他们处理事件的原理几乎万全一样,本文只用按钮来举例. 1.直接监听事件 把下面脚本直接绑定 ...

  8. 五, Authentication和Permissions

    概述 在介绍Django REST Framework(二):Request和Response 时提到,DRF提供了对身份验证和权限的处理机制,特点如下: 1.对API的不同部分使用不同的认证策略: ...

  9. 一,Serializer和ModelSerializer

    1.REST Framework概述 Django REST framework是一套基于Django的REST框架,是一个用于构建Web API的功能强大且灵活的工具包. RESTful 简述 Re ...

  10. SSM综合练习

    CRM系统 CRM项目外观 1. 开发环境 IDE: Eclipse Neon Release (4.6.0) Jdk: 1.8 数据库: MySQL 2. 创建数据库 创建crm数据库,这里使用的是 ...