- 浏览: 56687 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
shifulong:
cywhoyi 写道现在用Javassist代理也不错的看了一 ...
java动态代理(jdk &cglib) -
cywhoyi:
现在用Javassist代理也不错的
java动态代理(jdk &cglib)
Throwable
Exception
RuntimeException 可以捕获,也可以不捕获(NullPointerException,ArrayIndexOutOfBoundException等)
非运行时异常 必须捕获,否则编译报错
Error
编译器不要求强制处理(如OutOfMemoryError)
throw throws的区别
throw代码中使用,抛出异常
throws声明方法时使用
如果你的答案是这个,那就错了
i=2
i=1
testEx2, catch exception
testEx2, finally; return value=false
testEx1, catch exception
testEx1, finally; return value=false
testEx, catch exception
testEx, finally; return value=false
上面的测试代码有 两个坑
1,i/0 会抛出一个运行时异常即RunntimeException,可以捕获也可以不捕获
2,testEx2 catch 中的 throw 相当于一个结束语句,在执行完finally语句块之后才会被执行,
but...finally中 已经 return结束 (或者 再次throw),catch 中的 throw不会执行 也就不会抛出异常,testEx1中也不会接收异常
正确的打印结果
i=2
i=1
testEx2, catch exception
testEx2, finally; return value=false
testEx1, finally; return value=false
testEx, finally; return value=false
最后附一张高清图片
Exception
RuntimeException 可以捕获,也可以不捕获(NullPointerException,ArrayIndexOutOfBoundException等)
非运行时异常 必须捕获,否则编译报错
Error
编译器不要求强制处理(如OutOfMemoryError)
throw throws的区别
throw代码中使用,抛出异常
throws声明方法时使用
public class Test { public Test() { } 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() 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) { 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) { System.out.println("testEx2, catch exception"); ret = false; throw e; } finally { System.out.println("testEx2, finally; return value=" + ret); return ret; } } public static void main(String[] args) { String s = new String(new char[1 << 30]); Test test = new Test(); try { test.testEx(); } catch (Exception e) { e.printStackTrace(); } } }
如果你的答案是这个,那就错了
i=2
i=1
testEx2, catch exception
testEx2, finally; return value=false
testEx1, catch exception
testEx1, finally; return value=false
testEx, catch exception
testEx, finally; return value=false
上面的测试代码有 两个坑
1,i/0 会抛出一个运行时异常即RunntimeException,可以捕获也可以不捕获
2,testEx2 catch 中的 throw 相当于一个结束语句,在执行完finally语句块之后才会被执行,
but...finally中 已经 return结束 (或者 再次throw),catch 中的 throw不会执行 也就不会抛出异常,testEx1中也不会接收异常
正确的打印结果
i=2
i=1
testEx2, catch exception
testEx2, finally; return value=false
testEx1, finally; return value=false
testEx, finally; return value=false
最后附一张高清图片
发表评论
-
test
2016-02-26 17:45 433http://c.hiphotos.baidu.com/t ... -
java Integer的比较
2016-01-18 20:09 1034/** * A constant ... -
load过高排查
2016-01-07 19:15 1090jstack 线程的运行情况、线程的状态 load过高的 ... -
Runnable Callable Future FutrueTask ExecutorService的关系
2015-11-30 16:34 662public interface ExecutorSe ... -
单例模式以及序列化仍然单例的问题
2015-11-18 10:47 554这种方式仍然存在并发的同步阻塞问题,并且jdk1.5之 ... -
linux5种网络IO模型
2015-11-07 21:58 860看网上书上的各种解释太绕口难理解了,看到个例子挺好的。 ... -
nio学习记录 : start(buffer channel selector)
2015-11-06 22:17 727Buffer的capacity,limit,positio ... -
desgn partten : 适配器&门面
2015-11-05 11:55 535adapter adapter 一个接口转换成另一个接 ... -
java泛型中的PECS
2015-10-26 17:58 979extends List<? extends Nu ... -
spring自定义cache
2015-10-21 17:14 4511下面是一个较为详细的spring缓存的介绍 http:/ ... -
spring各种接口的作用FactoryBean、ApplicationContextAware、InitializingBean
2015-10-20 20:04 3007FactoryBean 当配置文件中<bean&g ... -
jvm调试命令
2015-10-14 15:42 465jps jps -mlv 列出当前jvm进程 ji ... -
CAS自定义线程安全的计数器
2015-10-10 17:36 2158写了一个线程安全的计数器(用锁显得多没有水平),非阻塞的,通过 ... -
java线程遇见的一个小问题(本质和线程无关都是基础知识)
2015-09-21 10:29 525猜猜下面几个程序,那个是线程安全的 NO1 public cl ... -
JUC--Parser
2015-09-08 16:27 655Java 7的并发包中推出了Phaser,其功能跟Cyclic ... -
JUC--Exchanger
2015-09-07 17:26 860Exchanger 用于线程之间数据交换,通过exchange ... -
java浮点型float遇到的一点问题
2015-04-10 16:32 644float 可以除以0,还可以不是一个数字 即Float.is ... -
java静态块的一个小坑
2015-04-07 18:26 599public class Student { pr ... -
java学习计划
2015-04-07 17:57 372刚入职的时候,team大神给制定的学习计划。 受益匪浅啊 ... -
购物网站(spring+mybatis+velocity+maven)项目
2015-04-03 18:06 955原文地址: http://www.iteye.com/top ...
相关推荐
java异常总结java异常总结java异常总结java异常总结java异常总结java异常总结
Java异常处理总结
JAVA基础测试中常见的异常问题汇总
java常见异常总结,这是在开发过程遇上的异常信息处理方式,相信对大家开发有一定的帮助!!
JAVA基础测试中常见的异常问题汇总.txt
java异常的终极总结 包括非常全面的异常总结 你值得下载!
Java常见异常汇总.pdf
java异常汇总.txt
Java 异常处理的误区和经验总结 本资料共包含以下附件: Java 异常处理的误区和经验总结.pdf
个人关于java异常处理的思维导图总结。0积分下载。如果认为有帮助,请关注点赞,日后还有更多免费资源。敬请关注。
java 异常种类总结【转】.docx java 异常种类总结【转】.docx java 异常种类总结【转】.docx
摘要使用Java异常的人很多,但能合理使用的却不多,Java异常处理设计是一个冷门的话题,但好的异常设计会让程序有质的变化,所以本文从各个方面分析便总结了,在J
java异常处理思维导图,自己根据尚硅谷的视频总结的思维导图,主要针对容易遗忘的知识点,方便自己记忆
Java异常处理总结.pdf
详细地总结了有关java异常,比如异常产生的原因,抛出异常的方法等等
JDK1.7以上的异常体系,需要后续的不断总结和完善,学习jdk必备!
java异常总结.pdf