java 異常抛出 throw 與 return
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的更多相关文章
- java中异常的抛出:throw throws
java中异常的抛出:throw throws Java中的异常抛出 语法: public class ExceptionTest{ public void 方法名(参数列表) throws 异常列表 ...
- java中抛出throw关键字是怎么用的? 举例?
5.抛出throw关键字 马克-to-win:我们先说5/0的原理,当程序运行到5/0的时候,java系统JVM会在后台new出一个除0异常实例,之后把这个实例传入catch块儿供开发者使用.马克-t ...
- java的异常抛出throws和throw的简单使用
前提: 当在程序测试时,如果你需要定义一个自己的异常,而非现在已经存在的异常,这个时候你需要用到throws和throw,try-catch只是一个简单的捕获异常的过程. 代码如下: package ...
- 【JAVASE】Java同一时候抛出多个异常
Java有异常抛出后.跳出程序.一般无法运行接下来的代码. 大家做登陆功能.常常会实username和password的登陆校验,username或者password错误.假设通常是提示usernam ...
- 【优化】自定义抛出throw 对象练习
package ltb6w; import java.util.*; public class Bank { private boolean bool=true; private String sel ...
- 自定义抛出throw 对象练习
package ltb6w; import java.util.*; public class Bank { private String select; private String select2 ...
- java的异常抛出和String类常用方法
一.异常抛出 异常是程序的异种非错误的意外情况,分为运行期异常(RuntimeException)和编译期异常(CheckedExcption) 处理异常可以用try——catch或自定义 impor ...
- java使用json抛出org.apache.commons.lang.exception.NestableRuntimeException解决方案
出现这个问题,说明缺少jar包,将下面的jar引入即可 commons-beanutils-1.8.3 commons-lang-2.6 (注:导入最新的 3.1 版本会继续报如下错误) common ...
- java-异常-原理异常对象的抛出throw
1 class Demo { 2 public static int method(int[] arr,int index) { 3 4 // System.out.println(arr[index ...
随机推荐
- as2 连接服务器 post
import mx.utils.Delegate; //接收服务器数据的文本加载器 var result_lv:LoadVars; /** * 数据提交成功后 * 获取的数据 * @param suc ...
- scala 爬虫 去除不能存储的特殊字符
scala 爬虫 去除不能存储的特殊字符 /** * 去除不能存储的特殊字符 */ def zifuChange(str: String): String = { var bo = true var ...
- java实现生成二维码
package com.cn.test; import java.awt.Graphics2D; import java.awt.geom.AffineTransform; import java.a ...
- Oracle部署安装
1.用户有数据库服务器则不需要安装Oracle服务器端,若没有需要安装 https://jingyan.baidu.com/article/363872eccfb9266e4aa16f5d.html ...
- C#中Graphics的画图代码【转】
我要写多几个字上去 string str = "Baidu"; //写什么字? Font font = Font("宋体",30f); //字是什么样子的? B ...
- Swift类型转换 和 类型别名的定义(typealias)
(一)类型转换 类型转化在 Swift 中是比较严格的,不同类型之间可以认为是不能相互转化的,只能重新产生一个对象和值,并拷贝一份. 1.0 整型数值之间的转换. // 不同类型是不能直接相加的,这时 ...
- thymeleaf 的内置对象
- HDFS中的集中缓存管理详解
一.背景 Hadoop设计之初借鉴GFS/MapReduce的思想:移动计算的成本远小于移动数据的成本.所以调度通常会尽可能将计算移动到拥有数据的节点上,在作业执行过程中,从HDFS角度看,计算和数据 ...
- 获取当前ip
测ip地址http://2018.ip138.com/ic.asphttps://www.ip.cn/
- 学JS的心路历程-Promise(一)
今天在进入Promise代码之前,我们先来用个例子来解释Promise是什么. 未来值 假设我们今天来到快餐店,点了一个汉堡,付钱给店员. 点了餐点并付费,可以理解为我们发送了一个请求,希望得到一个回 ...