异常
1.概念:在Java程序中可能导致程序意外失败的事件称为异常。
程序运行 --> 异常 --> 程序中断进程
2.类型
1)运行时磁盘空间不够
2)内存耗尽无法实例化
3)算术异常
4)jvm崩溃
5)…
3.异常体系结构
1)所有的异常类的祖先是Throwable
2)异常可以分为Error和Exception
----Error:错误 程序不进行处理
----Exception:异常 在程序开发中,要对这些异常进行处理
—编译时异常:在编译期间出现的异常
—运行时异常:在运行期间出现的异常
4.常见的异常
1)数组下标越界异常ArrayIndexOutOfBoundsException
int[] numbers = new int[5];
System.out.println(numbers[5]);
2)算术异常 ArithmeticException
int i = 10;
System.out.println(i/0);
3)空指针异常 NullPointerException
String names[] = null;
System.out.println(names[0]);
4)类型转化异常
Object date = new Date();
String string = (String)date;
5.处理异常方式:抓抛模型
1)抛:
当执行程序时,一旦出现异常,就会生成一个对应异常类型的对象,并将对象抛出。
----抛的类型:自动抛(运行时异常)、手动抛(throw + 异常类的对象)
----此异常的对象处理是交给方法的调用者。
下面是手动抛代码案例:
//比较两个圆的半径大小
public int compareTo(Object obj) {
if(this == obj) {
return 0;
}else if(obj instanceof Circle) {
Circle c = (Circle)obj;
if(this.radius > c.radius) {
return 1;
}else if(this.radius == c.radius) {
return 0;
}else {
return -1;
}
}else {
// 手动抛出异常
throw new RuntimeException("类型不匹配");
}
}
2) 抓:
在Java程序运行时收到异常对象,会寻找能处理这种类型的异常代码,并把当前异常对象交给其来处理。抓就是异常处理,两种方式:
– Try-catch
– Throws + 异常类型
try-catch-finally
(1)Try代码块里面存放的代码是有可能发生异常的代码,同时try中的代码中声明的变量是局部变量,作用就是在代码块中。
(2)Catch语句用来处理异常
– getMessage():获取异常信息
– printStackTrace():打印异常信息,(程序出错的位置信息及原因)
(3)catch代码块可以有多个,try中抛出的异常对象从上往下去匹配类型,一旦满足就执行catch的代码,执行完成后就跳出所有的catch块。
(4)如果catch中多个异常是并列关系,不需要考虑顺序问题。
(5)如果catch中多个异常有继承关系,那么子类需要在父类异常对象上面。
(6)finally是可选的代码块,如果加了finally,不论发生异常与否,finally一定执行
try{
//可能会发生的异常
}catch(Exception e1){
//处理方式一
}catch(Exception e1){
//处理方式二
}finally{
//一定要执行的代码
}
throws
1)在方法的声明处,显示的抛出异常对应的类型(委托处理)
2)语法格式:public void method() throw ClassCastException,FileNotFoundException
3) 语法剖析:
当此方法内出现异常时,会抛出一个异常类的对象,抛给方法的调用者。
异常对象是可以一直向上抛,直到main函数也可以通过try-catch进行异常处理。
public static void method3() {
try {
method2();
} catch (FileNotFoundException | InterruptedException e) {
e.printStackTrace();
}
}
public static void method2() throws FileNotFoundException, InterruptedException {
method1();
}
public static void method1() throws FileNotFoundException,InterruptedException{
FileInputStream fs = new FileInputStream(new File("hello.txt"));
Thread.sleep(200);
}
提示
1)运行时异常,可以不用显式的处理异常
2)编译时异常,必须要进行显示的处理
如何自定义异常:
1)自定义的异常类需要继承于现有的异常类(RuntimeException)
2)提供一个序列号,通过序列号可以唯一确定异常类的对象
3)需要提供一个重载的构造器
public class MyException extends RuntimeException{
private static final long sID = -571467842;
public MyException(String message) {
super(message);
}
}
练习异常的较好的例题:
package com.hpe.exception;
public class EcmDef {
public static void main(String[] args) {
try {
// 被除数
int i = Integer.parseInt(args[0]);
// 除数
int j = Integer.parseInt(args[1]);
ecm(i, j);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("数组下标越界,缺少命令行参数");
} catch(NumberFormatException e) {
System.out.println("数据类型不一致");
} catch(ArithmeticException e) {
System.out.println("分母不能为0");
}finally {
System.out.println("武汉加油");
}
}
/**
* 实现两个数相除的方法
* @param i
* @param j
*/
public static void ecm(int i,int j) {
// 两数不能为负数,负责抛出异常
if(i<0 || j<0) {
throw new MyException("输入的数字存在负数");
}
System.out.println(i / j);
}
}