Throwable exception; //类
Exception exce; //继承Throwable
Error err; //继承Throwable
RuntimeException re; //继承Exception, 运行时异常
//Exception与Error 虚拟机是如何处理的?
//throw new Error(); //程序中止
//throw new Exception();
//必须try..catch或者throw, 程序可以继续执行, 编译时检查
//运行时检查, 不需要捕获, 运行时如果发生这类异常,程序中止
//throw new RuntimeException();
/*
异常分为运行时异常和检查异常,除了RuntimeException, 其它的都是CheckedException.
运行时异常包括:
Java.lang.ArithmeticException
Java.lang.ArrayStoreExcetpion
Java.lang.ClassCastException
Java.lang.IndexOutOfBoundsException
Java.lang.NullPointerException
检查异常包括:
Java.lang.ClassNotFoundException
Java.lang.NoSuchMetodException
java.io.IOException
Error包括:
OutOfMemoryError
StackOverflowError
异常可以被程序处理,错误无法处理。
自定义异常:只需要继承Exception
*/
System.out.println("dd");
try{
//运行时检查, 不需要捕获,如果捕获,程序继续执行
throw new RuntimeException();
}catch(Exception e){
;
}
System.out.println("xx");
异常示例:
testEx方法没捕获抛出的异常?
《Thinking in Java》书中提到,finally语句块中如果重新抛出异常,会覆盖掉之前try块中捕获的异常,finally块中使用return会丢失try块中捕获的异常,当然finally块中,不能同时出现抛出异常和return语句
public class TestException {
public TestException() {
}
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) {
TestException testException1 = new TestException();
try {
testException1.testEx();
} catch (Exception e) {
e.printStackTrace();
}
}
}
相关推荐
java异常处理java异常处理java异常处理java异常处理java异常处理java异常处理java异常处理java异常处理java异常处理java异常处理java异常处理java异常处理java异常处理
高效的java异常处理框架高效的java异常处理框架高效的java异常处理框架高效的java异常处理框架高效的java异常处理框架
java异常练习题,北大青鸟学士后课后练习,主要是异常处理的小测验
java 异常详解,java异常分类,创建自己的异常
java异常处理的习题,撒个的是的风格成本凡人歌是东方按时到岗
Java异常学习资料,Java异常学习资料,Java异常学习资料
本资源主要描述java异常的知识,里面内容丰富,是java初学者的学习手册,java异常机制小结。
java异常的终极总结 包括非常全面的异常总结 你值得下载!
C++ Java异常处理比较
java 异常处理 代码实例java 异常处理 代码实例
有效处理JAVA异常三原则有效处理JAVA异常三原则有效处理JAVA异常三原则
超全的java异常体系结构以及异常的处理方案,Throwable->Exception-> RuntimeException
java学习之Java异常.ppt
java高端技术课程--Java异常处理详解java高端技术课程--Java异常处理详解java高端技术课程--Java异常处理详解java高端技术课程--Java异常处理详解
本表格集中了所有的java异常 并清晰的加以描述
Java异常详解_动力节点Java学院整理,动力节点口口相传的Java黄埔军校
个人关于java异常处理的思维导图总结。0积分下载。如果认为有帮助,请关注点赞,日后还有更多免费资源。敬请关注。
摘要使用Java异常的人很多,但能合理使用的却不多,Java异常处理设计是一个冷门的话题,但好的异常设计会让程序有质的变化,所以本文从各个方面分析便总结了,在J
Java论文异常处理论文:JAVA异常处理的分析与研究.doc
JAVA异常出错的教程,非常全面。 JAVA异常出错的教程,非常全面。