异常体系:
Throwable
---Error
一般出现重大问题:如类不存在或者内存溢出。中止运行,不对代码进行处理
---Exception
在运行时出现的状况,能够进行针对性处理。能够经过try catch finallyjava
---RuntimeException 运行时异常。函数
异常处理:this
try { 须要被检测的代码 } catch (异常类 变量) { 处理异常的代码 } finally { 必定会执行的代码
一般是关闭资源或链接。 }
例子:对象
class Div { private int a; private int b; public int div(int a,int b) { this.a=a; this.b=b; return a/b; } } class DivDemo { public static void main(String[] args) { try { Div d1=new Div(); int x=d1.div(6,0); System.out.println(x); } catch (Exception e) { System.out.println(e.getMessage());/// by zero System.out.println(e.toString());///java.lang.ArithmeticException: / by zero e.printStackTrace();//详细的堆栈信息 //java.lang.ArithmeticException: / by zero // at Div.div(Divdemo.java:9) // at DivDemo.main(Divdemo.java:22) } } }
对多异常的处理:
1.声明异常时,建议声明更为具体的异常。这样处理的能够具体些
2.对方声明几个异常,就对应有几个catch块,不要定义多余的catch块。
3.若是多个catch块中的异常出现继承关系,父类异常必定要放在最下面blog
自定义异常:
当函数内部出现了throw抛出异常对象,那么就必须给对应的处理动做。
1.要么在内部try catch处理
2.要么在函数上声明让调用者处理
3.如何自定义异常信息:由于父类中已经异常信息的操做都完成了。因此子类只要在构造时,讲异常信息传递给父类,经过super语句。继承
通常状况,函数内部出现异常,函数上须要声明。接口
4.自定义类必须继承Exception.
5.throws和throw的区别:
1)throws 使用在函数上,用于函数声明;throw 使用在函数内。
2)throws 后面紧跟着声明的异常类,能够多个,用逗号隔开;throw后面跟着是抛出异常类的对象。内存
6.catch 是用于处理异常。若是没有catch就表明异常没有被处理过,若是该异常是检测时异常,那么必须声明资源
7.finall一种状况下不会执行,当程序执行到System.exit(0),finally不会执行。例如:get
class Test { public static String output=""; public static void foo(int i) { try { if(i==1) throw new Exception(); output+="1"; } catch(Exception e) { output+="2"; return; //returun后finally继续执行,可是其余语句就再也不执行了 } finally { output+="3"; } output+="4"; } public static void main(String args[]) { foo(0); System.out.println(output);//134 foo(1); System.out.println(output); //13423 } }
8.throw单独存在,下面不要定义语句,由于执行不到。
class FushuException extends Exception { FushuException(String msg) { super(msg); } } class Div { private int a; private int b; public int div(int a,int b) throws FushuException//函数内部出现throw抛出异常,函数上须要throws声明。 { this.a=a; this.b=b; if (b<0) { throw new FushuException("出现了负数");
//System.out.print("dddd");//throw单独存在,下面不要定义语句,由于执行不到。 } return a/b; } } class FuDemo { public static void main(String[] args) { try { Div D1=new Div(); D1.div(3,-1); } catch (FushuException e) { System.out.print(e.toString()); } } }
Exception 中有一个特殊的子类异常叫RuntimeException 运行时异常。特色:出现了没法运行的状况,须要对代码进行修改
1.若是在函数内throw抛出异常,函数上能够不用声明,编译同样经过。
2.若是在函数上声明了该异常,调用者能够不用进行处理,编译同样经过。
自定义异常时:若是该类的发生,没法在继续进行运行,可以让自定义异常类继承RuntimeException
异常分类:
1.编译时被检测的异常
2.编译时不被检测的异常(RuntimeException以及子类)
catch内,须要定义针对性的处理方式。不要简单的定义printStackTrace,输出语句。 也不要不写。 当捕获到的异常,本功能处理不了时,能够继续在catch中抛出。 try { throw new AException(); } catch (AException e) { throw e; } 若是该异常处理不了,但并不属于该功能出现的异常。 能够将异常转换后,在抛出和该功能相关的异常。 或者异常能够处理,当须要将异常产生的和本功能相关的问题提供出去, 当调用者知道。并处理。也能够将捕获异常处理后,转换新的异常。 try { throw new AException(); } catch (AException e) { // 对AException处理。 throw new BException(); } 好比,汇款的例子。
异常在子父类覆盖中的体现:
1.子类在覆盖父类时,若是父类的方法抛出异常,那么子类的覆盖方法,只能抛出父类的异常或者该异常的子类。
2.若是父类方法抛出多个异常,那么子类在覆盖该方法时,只能抛出父类异常的子集。
3.若是父类或者接口的方法没有异常抛出,那么子类在覆盖方法时,也不能够抛出异常。
若是子类方法发生了异常,必须进行try处理,绝对不能抛
class NoValueException extends RuntimeException { NoValueException(String message) { super(message); } } interface Shape { void getArea(); } class Rec implements Shape { private int len,wid; Rec(int len ,int wid)//throws NoValueException { if(len<=0 || wid<=0) throw new NoValueException("出现非法值"); this.len = len; this.wid = wid; } public void getArea() { System.out.println(len*wid); } } class Circle implements Shape { private int radius; public static final double PI = 3.14; Circle(int radius) { if(radius<=0) throw new NoValueException("非法"); this.radius = radius; } public void getArea() { System.out.println(radius*radius*PI); } } class ExceptionTest1 { public static void main(String[] args) { Rec r = new Rec(3,4); r.getArea(); Circle c = new Circle(-8); System.out.println("over"); } }