Java 常用类解析:java异常机制,异常栈,异常处理方式,异常链,异常丢失详解
1、java标准异常概述
throwable表示任何可以作为异常被抛出的类,有两个子类error和exception。从这两个类的源代码中可以看出,这两个类并没有添加新的方法,throwable提供了所以方法的实现。error表示编译时和系统错误。exception是可以被抛出的异常类。runtimeexception继承自exception(如nullpointerexception),表示运行时异常,jvm会自动抛出.
2、自定义异常类
自定义异常类方法: 通过继承throwable或exception。异常类的所有实现都是基类throwable实现的,所以构造自定义异常类完全可以参考exception和error类。我们只要添加上自定义异常类的构造方法就可以了
<span style="font-size:16px;">package demo.others; /** * 自定义异常类方法 * 1、通过继承throwable * 2、通过继承exception * * @author touch */ public class myexceptiondemo extends exception { private static final long serialversionuid = 1l; public myexceptiondemo() { super(); } public myexceptiondemo(string message) { super(message); } public myexceptiondemo(string message, throwable cause) { super(message, cause); } public myexceptiondemo(throwable cause) { super(cause); } } </span>
3、异常栈及异常处理方式
可以通过try、catch来捕获异常。捕获到的异常。下面的示例演示了几种常用异常处理方式
<span style="font-size:16px;">package demo.others; import mine.util.exception.myexception; public class exceptiondemo1 { public void f() throws myexception { throw new myexception("自定义异常"); } public void g() throws myexception { f(); } public void h() throws myexception { try { g(); } catch (myexception e) { //1、通过获取栈轨迹中的元素数组来显示异常抛出的轨迹 for (stacktraceelement ste : e.getstacktrace()) system.out.println(ste.getmethodname()); //2、直接将异常栈信息输出至标准错误流或标准输出流 e.printstacktrace();//输出到标准错误流 e.printstacktrace(system.err); e.printstacktrace(system.out); //3、将异常信息输出到文件中 //e.printstacktrace(new printstream("file/exception.txt")); //4、重新抛出异常,如果直接抛出那么栈路径是完整的,如果用fillinstacktrace() //那么将会从这个方法(当前是h()方法)作为异常发生的原点。 //throw e; throw (myexception)e.fillinstacktrace(); } } public static void main(string[] args) { try { new exceptiondemo1().h(); } catch (myexception e) { // todo auto-generated catch block e.printstacktrace(); } } } </span>
运行结果:
f g h main mine.util.exception.myexception: 自定义异常 at demo.others.exceptiondemo1.f(exceptiondemo1.java:7) at demo.others.exceptiondemo1.g(exceptiondemo1.java:11) at demo.others.exceptiondemo1.h(exceptiondemo1.java:16) at demo.others.exceptiondemo1.main(exceptiondemo1.java:35) mine.util.exception.myexception: 自定义异常 at demo.others.exceptiondemo1.f(exceptiondemo1.java:7) at demo.others.exceptiondemo1.g(exceptiondemo1.java:11) at demo.others.exceptiondemo1.h(exceptiondemo1.java:16) at demo.others.exceptiondemo1.main(exceptiondemo1.java:35) mine.util.exception.myexception: 自定义异常 at demo.others.exceptiondemo1.f(exceptiondemo1.java:7) at demo.others.exceptiondemo1.g(exceptiondemo1.java:11) at demo.others.exceptiondemo1.h(exceptiondemo1.java:16) at demo.others.exceptiondemo1.main(exceptiondemo1.java:35) mine.util.exception.myexception: 自定义异常 at demo.others.exceptiondemo1.h(exceptiondemo1.java:30) at demo.others.exceptiondemo1.main(exceptiondemo1.java:35)
分析上面的程序,首先main函数被调用,然后是调用h函数,再g函数、f函数,f函数抛出异常,并在h函数捕获,这时将依次从栈顶到栈底输出异常栈路径。
4、异常链
有时候我们会捕获一个异常后在抛出另一个异常,如下代码所示:
<span style="font-size:16px;">package demo.others; import java.io.ioexception; import mine.util.exception.myexception; public class exceptiondemo2 { public void f() throws myexception { throw new myexception("自定义异常"); } public void g() throws exception { try { f(); } catch (myexception e) { e.printstacktrace(); throw new exception("重新抛出的异常1"); } } public void h() throws ioexception { try { g(); } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); throw new ioexception("重新抛出异常2"); } } public static void main(string[] args) { try { new exceptiondemo2().h(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } } </span>
运行结果:
mine.util.exception.myexception: 自定义异常 at demo.others.exceptiondemo2.f(exceptiondemo2.java:9) at demo.others.exceptiondemo2.g(exceptiondemo2.java:14) at demo.others.exceptiondemo2.h(exceptiondemo2.java:23) at demo.others.exceptiondemo2.main(exceptiondemo2.java:32) java.lang.exception: 重新抛出的异常1 at demo.others.exceptiondemo2.g(exceptiondemo2.java:17) at demo.others.exceptiondemo2.h(exceptiondemo2.java:23) at demo.others.exceptiondemo2.main(exceptiondemo2.java:32) java.io.ioexception: 重新抛出异常2 at demo.others.exceptiondemo2.h(exceptiondemo2.java:27) at demo.others.exceptiondemo2.main(exceptiondemo2.java:32)
从结果中我们可以看出,异常栈变小了。也就是说丢失了最原始的异常信息。怎样保存最原始的异常信息呢?throwable类中有个throwable cause属性,表示原始异常。通过接收cause参数的构造器可以把原始异常传递给新异常,或者通过initcause()方法。如下示例:
<span style="font-size:16px;">package demo.others; import java.io.ioexception; import mine.util.exception.myexception; public class exceptiondemo2 { public void f() throws myexception { throw new myexception("自定义异常"); } public void g() throws exception { try { f(); } catch (myexception e) { e.printstacktrace(); throw new exception("重新抛出的异常1",e); } } public void h() throws ioexception { try { g(); } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); ioexception io=new ioexception("重新抛出异常2"); io.initcause(e); throw io; } } public static void main(string[] args) { try { new exceptiondemo2().h(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } } </span>
结果:
mine.util.exception.myexception: 自定义异常 at demo.others.exceptiondemo2.f(exceptiondemo2.java:9) at demo.others.exceptiondemo2.g(exceptiondemo2.java:14) at demo.others.exceptiondemo2.h(exceptiondemo2.java:23) at demo.others.exceptiondemo2.main(exceptiondemo2.java:34) java.lang.exception: 重新抛出的异常1 at demo.others.exceptiondemo2.g(exceptiondemo2.java:17) at demo.others.exceptiondemo2.h(exceptiondemo2.java:23) at demo.others.exceptiondemo2.main(exceptiondemo2.java:34) caused by: mine.util.exception.myexception: 自定义异常 at demo.others.exceptiondemo2.f(exceptiondemo2.java:9) at demo.others.exceptiondemo2.g(exceptiondemo2.java:14) ... 2 more java.io.ioexception: 重新抛出异常2 at demo.others.exceptiondemo2.h(exceptiondemo2.java:27) at demo.others.exceptiondemo2.main(exceptiondemo2.java:34) caused by: java.lang.exception: 重新抛出的异常1 at demo.others.exceptiondemo2.g(exceptiondemo2.java:17) at demo.others.exceptiondemo2.h(exceptiondemo2.java:23) ... 1 more caused by: mine.util.exception.myexception: 自定义异常 at demo.others.exceptiondemo2.f(exceptiondemo2.java:9) at demo.others.exceptiondemo2.g(exceptiondemo2.java:14) ... 2 more
从结果中看出当获取到“重新抛出异常2的时候,同时可以输出原始异常“重新抛出的异常1“和原始异常”自定义异常,这就是异常链。
5、finally的使用
finally子句总是执行的,通常用来做一些清理工作,如关闭文件,关闭连接等
下面举几个finally的例子:
<span style="font-size:16px;">// 读取指定路径文本文件 public static string read(string filepath) { stringbuilder str = new stringbuilder(); bufferedreader in = null; try { in = new bufferedreader(new filereader(filepath)); string s; try { while ((s = in.readline()) != null) str.append(s + '\n'); } finally { in.close(); } } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } return str.tostring(); }</span>
分析:如果调用in = new bufferedreader(new filereader(filepath));时发生异常,这时是一个文件路径不存在的异常,也就是说并没有打开文件,这时将会直接跳到catch块,而不会执行try...finally块(并不是finally子句)里面的语句in.close();此时不需要关闭文件。
再看一个例子,会导致异常的丢失
<span style="font-size:16px;">package demo.others; import mine.util.exception.myexception; public class exceptiondemo3 { public void f() throws myexception { throw new myexception("异常1"); } public void g() throws myexception { throw new myexception("异常2"); } public static void main(string[] args) { try { exceptiondemo3 ex = new exceptiondemo3(); try { ex.f(); } finally { ex.g();//此时捕获g方法抛出的异常,f方法抛出的异常丢失了 } } catch (myexception e) { system.out.println(e); } } } </span>
结果:mine.util.exception.myexception: 异常2
此时异常1就丢失了
或者这样写:
<span style="font-size:16px;">package demo.others; import mine.util.exception.myexception; public class exceptiondemo3 { public void g() throws myexception { throw new myexception("异常2"); } public static void main(string[] args) { exceptiondemo3 ex = new exceptiondemo3(); try { ex.g(); } finally { //直接return会丢失所以抛出的异常 return; } } } </span>
6、异常的限制
(1)当覆盖方法时,只能抛出在基类方法的异常说明里列出的那些异常,有些基类的方法声明抛出异常其实并没有抛出异常,这是因为可能在其子类的覆盖方法中会抛出异常
(2)构造器可以抛出任何异常而不必理会基类构造器所抛出的异常,派生类构造器异常说明必须包含基类构造器异常说明,因为构造派生类对象时会调用基类构造器。此外,派生类构造器不能捕获基类构造器抛出的异常。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!