java异常处理
异常分类
异常体系结构
java.lang.Throwable
|-----java.lang.Error:一般不编写针对性的代码进行处理。
|-----java.lang.Exception:可以进行异常的处理
|------编译时异常(checked)
|-----IOException
|-----FileNotFoundException
|-----ClassNotFoundException
|------运行时异常(unchecked,RuntimeException)
|-----NullPointerException
|-----ArrayIndexOutOfBoundsException
|-----ClassCastException
|-----NumberFormatException
|-----InputMismatchException
|-----ArithmeticException
Error
这是Java虚拟机无法解决的严重问题。这类异常不是我们能处理的,我们就不用处理了,直接改代码。
Exception
这种异常是因为其它因编程错误或偶然的外在因素导致的一般性问题,可以使
用针对性的代码进行处理。
例如:空指针访问、试图读取不存在的文件、网络连接中断、数组角标越界等。
这类异常又分为两类:运行时异常和编译时异常。
运行时异常
这类异常是编译器不要求强制处置的异常。一般来说是编程时的逻辑错误。
java.lang.RuntimeException
类及它的子类都是运行时异常。
编译时异常
是指编译器要求必须处置的异常。即程序在运行时由于外界因素造成的一
般性异常。编译器要求Java程序必须捕获或声明所有编译时异常。
异常处理
异常处理采用抓抛模型
过程一,“抛”:
程序在正常执行的过程中,一旦出现异常,就会在异常代码处生成一个对应异常类的对象,并将此对象抛出。
一旦抛出对象以后,其后的代码就不再执行。
过程二,“抓”:
可以理解为异常的处理方式:①try-catch-finally ②throws
try-catch-finally 的使用
try{
//可能出现异常的代码
}catch(异常类型1 变量名1){
//处理异常的方式一
}catch(异常类型2 变量名2){
//处理异常的方式二
}.....
finally{
//一定会执行的代码
}
-
说明:
-
finally是可选的。
-
使用try将可能出现异常的代码包装起来,在执行过程中,一旦出现异常,就会生成一个对应异常类的对象,根据此对象的类型,去catch中进行匹配。
-
一旦try中的异常对象匹配到某一个catch时,就进入catch中进行异常的处理。一旦处理完成,就跳出当前的try-catch结构(在没有写finally的情况)。继续执行后面的代码。
-
catch中的异常类型如果没有子父类关系,则谁声明在上,谁声明在下都无所谓。
catch中的异常类型如果满足子父类关系,则要求子类一定声明在父类的上面,否则报错。
-
常用的异常对象的处理方式:
①
String getMessage()
显示那个操作比如输入的什么或者角标出错了具体用法:
System.out.println(e.getMessage());
②
printfStackTrace()
显示这一块都具体调用了哪些机构具体用法:
e.printStackTrace();
-
在try结构中声明的变量,再出了try结构以后,就不能再被调用.
- 使用try-catch-finally处理编译时异常,使得程序在编译时就不再报错,但是运行时仍可能报错。相当于我们使用try—catch—finally将一个编译时可能出现的异常,延迟到运行时出现。
- 开发中,由于运行时异常比较常见,所以我们通常就不针对运行时异常编写try—catch—finally了。针对于编译时异常,我们一定要考虑异常的处理。
public class ExceptionTest { public static void main(String[] args) { int[] a = new int[3]; int c =4; int b=0; try{ System.out.println(c/b); }catch(ArrayIndexOutOfBoundsException e){ e.printStackTrace(); }catch(Exception e){ System.out.println("6666666"); } } }
-
-
try-catch-finally中finally的使用
- finally是可选的。
- finally中声明的是一定会被执行的代码。即使catch中又出现异常导致程序直接整个退出,或者时try中有return语句,catch中有return语句等情况。
- 像数据库连接、输入输出流、网络编程Socket等资源,JVM是不能自动的回收的,我们需要自己手动的进行资源的释放。此时的资源释放,就需要声明在finally中。
具体代码:
public class FinallyTest { public static void main(String[] args) { Pig p = new Pig(); int num = p.method(); System.out.println(num); } } class Pig{ int method(){ try{ System.out.println("感觉良好,准备出圈"); int[] a = new int[3]; System.out.println(a[4]); return 1; }catch(ArrayIndexOutOfBoundsException e){ System.out.println(4/0); System.out.println("我也不知道咋办"); return 2; }finally{ System.out.println("别问,问就finally"); return 3; } } }
throws的使用
-
’throws + 异常类型‘写在方法的声明处。指明此方法执行时,可能会抛出的异常类型。
一旦当方法体执行时,出现异常,仍会在异常代码处生成一个异常类的对象,此对象满足throws后异常类型时,就会被抛出。异常代码后续的代码,就不再执行。
-
体会:try-catch-finally:真正的将异常给处理掉了。并没有真正将异常处理掉。
-
开发中如何选择使用try-catch-finally还是throw?
- 如果父类中被重写的方法没有throws方式处理异常,则子类重写的方法也不能使用throws。因为多态的原因,形参为父类的话,子类也能使用,但因为父类无异常,子类如果有异常的话处理不了。同理,如果父类中有处理异常的手段,那么子类的异常必须小于等于父类中的异常,不然父类出料不掉。如果父类无异常,但子类中一定要有异常的话,必须使用try-catch-finally方式处理。
- 执行的方法a中,先后又调用了另外的几个办法,这几个方法是递进关系执行的。我们建议这几个方法使用throws的方式进行处理。而执行的方法a可以考虑使用try-cathch-finally方式进行处理。不然如果递进的方法中有一个方法使用了try-cathch-finally处理,但传递的值可能会出错,但程序还是继续执行,还不如用throws结束运行。而执行a的方法可以考虑使用try-cathch-finally方式进行处理。
public class ExceptionTest2 {
public static void main(String[] args){
try{
method2();
}catch(IOException e){
e.printStackTrace();
}
// method3();
}
public static void method3(){
try {
method2();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void method2() throws IOException{
method1();
}
public static void method1() throws FileNotFoundException,IOException{
File file = new File("hello1.txt");
FileInputStream fis = new FileInputStream(file);
int data = fis.read();
while(data != -1){
System.out.print((char)data);
data = fis.read();
}
fis.close();
System.out.println("hahaha!");
}
}
throw自定义异常
用于抛出异常。
- 自定义异常的流程
- 继承于现有的异常结构RuntimeException 、Exception
- 提供全局常量:serialVersionUID
- 提供重载的构造器
自定义异常
public class MyException extends Exception {
static final long serialVersionUID = -3364566993124229948L;
public MyException(){
}
public MyException(String msg){
super(msg);
}
}
测试
public class MyExceptionTest {
public static void main(String[] args) {
try {
shoudong();
} catch (MyException e) {
System.out.println(e.getMessage());
}
}
public static void shoudong() throws MyException{
System.out.println("我要发抛了");
throw new MyException("我自定义的异常");
}
}
课后练习
/*
- 编写应用程序EcmDef.java,接收命令行的两个参数,要求不能输入负数,计算两数相除。
对数据类型不一致(NumberFormatException)、缺少命令行参数(ArrayIndexOutOfBoundsException、
除0(ArithmeticException)及输入负数(EcDef 自定义的异常)进行异常处理。
提示:
(1)在主类(EcmDef)中定义异常方法(ecm)完成两数相除功能。
(2)在main()方法中使用异常处理语句进行异常处理。
(3)在程序中,自定义对应输入负数的异常类(EcDef)。
(4)运行时接受参数 java EcmDef 20 10 //args[0]=“20” args[1]=“10”
(5)Interger类的static方法parseInt(String s)将s转换成对应的int值。
如:int a=Interger.parseInt(“314”); //a=314;
*/
自定义异常
public class FushuException extends Exception{
static final long serialVersionUID = -338751699356729948L;
public FushuException(){
}
public FushuException(String msg){
super(msg);
}
}
测试
public class FushuExceptionTest {
public static void main(String[] args) {
try {
int i = new Integer(args[0]);
int j = new Integer(args[1]);
System.out.println(method(i,j));
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("缺少命令行参数");
} catch (NumberFormatException e) {
System.out.println("数据类型不一致");
} catch (ArithmeticException e) {
System.out.println("除0了");
} catch (FushuException e) {
System.out.println(e.getMessage());
}
}
public static int method (int a,int b) throws FushuException{
if(a<0 || b<0){
throw new FushuException("俩数里有负数");
}
return a/b;
}
}