异常处理
java中的异常
异常处理概述:
异常就是程序在执行时产生的不正常的现象,例如,在做除法运算时,0时不能做除数的,计算机会通过异常将这个信息输出,以此进行说明,java中的异常可以通过关键字进行控制,主要有try,catch,throw,throws,finally五个,下面进行一一讲解:
(1)try:使用在程序可能出现异常的地方,将程序段包起来,如果try语句中出现异常,那么这个异常就会被抛出。
(2)catch:来捕获异常,并在这个语句块中对该异常进行异常处理。public class test02 {
public static void main(String[] args){
int a=0,b=1,c;
try{
c=b/a;
}catch (Exception e1){
System.out.println("此处发生exception异常");
}
}
}
(3)finally:不管发生还是不发生都进行异常处理public class test02 {
public static void main(String[] args){
int a=1,b=1,c=0;
try{
c=b/a;
}catch (Exception e1){
System.out.println("此处发生exception异常,分母为零");
}
finally{
System.out.println("不管是否发生异常,都会进行处理");
}
System.out.println(c);
}
}
(4)throw:用来动手引发一个异常
(5)throws:声明一个可以方法,该方法可以抛出异常。
异常的结构:
异常的体系非常庞大,可将异常分为未检查异常和已检查异常,所有的异常都是继承的Throwable接口。
java中异常主要有三种,也就是Throwable的三个基本子类
Error一般表示运行时内部错误,以及资源耗尽,表示很难恢复的错误,一般不期望用户来进行处理。Expection:异常的父类,程序小合集不合理所导致java认为可以恢复。RuntineExpection,用来表示设计或实现方面的错误,如数组越界,类型转换错误。
- 类型转换异常
public class test02 { public static void main(String[] args){ test02 t=new test02(); book b=t.new book(); String str=b; } class book { String num; } } Exception in thread "main" java.lang.Error: 无法解析的编译问题: 类型不匹配:不能从 test02.book 转换为 String at test01.test02.main(test02.java:7)
- 数组访问越界
- 空指针访问异常
怎样处理异常public class test02 { public static void main(String[] args){ test02 t=new test02(); book b=null; b.display(); } class book { String num; public void display(){ System.out.pritntln(num); } } } Exception in thread "main" java.lang.NullPointerException at test01.test02.main(test02.java:7)
出现异常的时候,(1)可以直接进行捕获处理(2)将他抛给上面的调用者try-catch算是比较简单的异常捕获处理了,话不多说直接上例子。
异常处理 try-catch捕获异常 对应还有多个catch语句块的情况 throws声明异常 throw抛出异常 自定义异常 声明异常,此时此刻你看到声明异常的时候想到什么了吗,竟然可以声明!!!感觉功能越来越强大了。。。public class test02 { public static void main(String[] args){ int a=3,b=0,c=0; try{ c=a/b; }catch(Exception e){ System.out.println("发生异常,分母竟然为零"); } } } 程序运行:发生异常,分母竟然为零
不扯淡了:先讲讲什么意思吧:声明异常就是指一个方法不处理他所产生的异常,而是延调用层次向上传递,谁调用这个方法,这个异常就由谁来进行处理。
emm,好像知道了,就是声明这个异常的方法是不管闲事的,谁调用出了问题谁自己解决,反正这个方法不管了,,,,好吧。到了throw抛出异常了,,,,package test01; public class test02 { public void method(int a) throws ArrayIndexOutOfBoundsException,ArithmeticException{ System.out.println(a); if(a==0){ System.out.println("没有异常哦"); } if(a==1){ int [] x=new int [3]; x[3]=4; } if(a==2){ int j=0; System.out.println(5/a); } } public static void main(String[] args){ test02 t=new test02(); try{t.method(1);}catch(Exception e){ System.out.println("出错了"+e);//反正方法中不管,,,, } } } 1 出错了java.lang.ArrayIndexOutOfBoundsException: 3
throw在具体方法中抛出异常,他的语法格式如下。
throw exception;
throw是关键字,参数exception表示将要抛出的异常的对象public class test02 { public void throwException()throws Exception { String str=null; if(str==null){ throw new Exception("str==null"); } } public static void main(String[] args){ test02 t=new test02(); try{ t.throwException(); }catch(Exception e){ System.out.println("emm确实是为空"+e); } } } emm确实是为空java.lang.Exception: str==null
用户自定义异常类
异常类的定义非常简单,只需要继承Exception,并实现一些方法就行了,创建异常类的方法一般如下
class 类名extends Exception{
//类体
}
捕捉多个异常
当有多个异常的时候,catch语句是按照从上往下来进行,当上边捕捉到异常后,后面的异常就不在进行捕捉
package praitse; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class Main { public static void main(String[] args) { File file=new File("d:\\a.txt"); try { FileInputStream in=new FileInputStream(file); } catch(IOException a) { System.out.println("这是异常问题"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
这个程序编译阶段会报错Exception in thread "main" java.lang.Error: Unresolved compilation problem: Unreachable catch block for FileNotFoundException. It is already handled by the catch block for IOException at praitse.Main.main(Main.java:17)
异常的继承图示: