讲解知识点
* 什么是异常
* 异常是怎么产生的
* 异常的结构
* 异常的处理机制
* 常见的异常
具体内容
引入:
public class TestException {
public TestException() {
}
@SuppressWarnings("finally")
boolean testEx() throws Exception {
boolean ret = true;
try {
ret = testEx1();
} catch (Exception e) {
System.out.println("testEx, catch exception");
ret = false;
throw e;
} finally {
System.out.println("testEx, finally; return value=" + ret);
return ret;
}
}
@SuppressWarnings("finally")
boolean testEx1() throws Exception {
boolean ret = true;
try {
ret = testEx2();
if (!ret) {
return false;
}
System.out.println("testEx1, at the end of try");
return ret;
} catch (Exception e) {
System.out.println("testEx1, catch exception");
ret = false;
throw e;
} finally {
System.out.println("testEx1, finally; return value=" + ret);
return ret;
}
}
@SuppressWarnings("finally")
boolean testEx2() throws Exception {
boolean ret = true;
try {
int b = 12;
int c;
for (int i = 2; i >= -2; i--) {
c = b / i;
System.out.println("i=" + i);
}
return true;
} catch (Exception e) {
System.out.println("testEx2, catch exception");
ret = false;
throw e;
} finally {
System.out.println("testEx2, finally; return value=" + ret);
return ret;
}
}
public static void main(String[] args) {
TestException testException1 = new TestException();
try {
testException1.testEx();
} catch (Exception e) {
e.printStackTrace();
}
}
}
输出结果:
i=2
i=1
testEx2, catch exception
testEx2, finally; return value=false
testEx1, finally; return value=false
testEx, finally; return value=false
注意: 在finally语句中不应该出现return,在return执行的时候方法已经结束。
什么是异常 ?
在程序运行时出现的错误。即为异常。
(异常是在程序运行中导致程序中断运行的一种指令流)
异常是怎么产生的?
异常是不期而至的各种情况,网络中断,非法参数,找不到文件等都会产生异常。 异常是一个事件,它干扰了程序的正常运行。 当异常出现时会产生一个异常的对象。
异常的结构
Throwable : 是异常的始祖。包含两个重要的分支子类 ,子类又包含了大量的子类。
Error(错误) : 是程序无法处理的错误。表示程序中比较严重的错误,大多数情况下与代码编写者无关,是jvm 出现了问题。Java虚拟机一般会选择线程终止。
Exception(异常): 是程序本身可以处理的异常,分为受检异常和非受检异常。 受检异常是需要人为处理的(非运行时异常)。 非受检异常是由系统处理的。(报错 改正代码)
处理异常的机制
1、处理异常
在java程序中,异常处理机制分为:抛出异常、捕获异常。
2、捕获异常
1、catch 只会执行一个。
2、Finally 可以不写
3、前面的catch类型一定要比后面的范围小(否则没有意义)
4、当try语句或者catch中遇到return,finally的类容将在方法返回(return)之前执行。
5、特殊情况下finally不执行
1)finally发生异常
2)System.exit() (System.exit(0) : 正常结束程序 ;System.exit(1) : 异常关闭程序。)且在finally前执行
3)程序所在的线程死了
4)关闭CPU
实例:
public class ExceptionDemo01 {
public static void main(String[] args) {
int i = 10;
int j = 0;
try {
int temp = i/j;
System.out.println(temp);
} catch (ArithmeticException e) {
e.printStackTrace();//打印异常
}catch (Exception e) {
e.printStackTrace();
}finally{
System.out.println("finally");
//总是执行
}
//java.lang.ArithmeticException 算术条件异常
}
}
6、try、catch、finally 之间的关系
Try catch 可以有多个catch ;
Try finally 也可以使用
Try catch 可以嵌套使用
Catch finally 不能使用。
(catch是通过try产生的异常的对象进行比对在捕获异常并输出,所以没有try中异常的对象 catch就不能执行。)
抛出异常
1、任何代码都可以抛出异常,如果一个方法中可能出现异常,但没有能力处理这种异常,可以在申明方法的地方用throws 关键字抛出异常。
Throws 使用格式
Public 返回值类型 方法名称(参数列表) throws 异常类{ }
实例:
class Math{
public int div(int i ,int j) throws Exception{
int temp = i/j;
System.out.println(temp);
return temp;
}
}
遇到异常可以抛出,如果调用这个方法,需要对这个方法进行异常处理,抛出或者捕捉。
异常的处理流程
1、一旦产生异常,系统就会自动产生一个异常类的实例化对象。
2、那么,此时如果存在try语句。则会自动找到配套的catch语句执行,如果没有异常处理,则程序将退出,并由系统报告错误。
3、所有的catch根据方法的参数匹配异常类的实例化对象,如果匹配成功,则表示此catch进行处理。(对父类进行处理相对方便)
Throw 关键字
表示人为的抛出一个异常(没有意义,抛着玩)
实例:
public class ExceptionDemo11{ public static void main(String argsp[]){ // 所有的异常交由JVM进行处理 try{ throw new Exception("抛着玩的。") ; // 人为的抛出异常 }catch(Exception e){ e.printStackTrace() ; } } } |
常见的异常
在Java中提供了一些异常用来描述经常发生的错误,对于这些异常,有的需要程序员进行捕获处理或声明抛出,有的是由Java虚拟机自动进行捕获处理。Java中常见的异常类:
1. runtimeException子类:
1、 java.lang.ArrayIndexOutOfBoundsException
数组索引越界异常。当对数组的索引值为负数或大于等于数组大小时抛出。
2、java.lang.ArithmeticException
算术条件异常。譬如:整数除零等。
3、java.lang.NullPointerException
空指针异常。当应用试图在要求使用对象的地方使用了null时,抛出该异常。譬如:调用null对象的实例方法、访问null对象的属性、计算null对象的长度、使用throw语句抛出null等等
4、java.lang.ClassNotFoundException
找不到类异常。当应用试图根据字符串形式的类名构造类,而在遍历CLASSPAH之后找不到对应名称的class文件时,抛出该异常。
5、 java.lang.NegativeArraySizeException 数组长度为负异常
6、 java.lang.ArrayStoreException 数组中包含不兼容的值抛出的异常
7、 java.lang.SecurityException 安全性异常
8、 java.lang.IllegalArgumentException 非法参数异常
2.IOException
IOException:操作输入流和输出流时可能出现的异常。
EOFException 文件已结束异常
FileNotFoundException 文件未找到异常
3. 其他
ClassCastException 类型转换异常类
ArrayStoreException 数组中包含不兼容的值抛出的异常
SQLException 操作数据库异常类
NoSuchFieldException 字段未找到异常
NoSuchMethodException 方法未找到抛出的异常
NumberFormatException 字符串转换为数字抛出的异常
StringIndexOutOfBoundsException 字符串索引超出范围抛出的异常
IllegalAccessException 不允许访问某类异常
InstantiationException 当应用程序试图使用Class类中的newInstance()方法创建一个类的实例,而指定的类对象无法被实例化时,抛出该异常
总结:
1、 异常的出现,如果没有进行合理的处理,则有可能造成程序的非正常结束。
2、 使用 try…catch、try…catch…finally完成异常的基本处理
· 在 try中捕获异常
· 交给 catch 进行匹配
· 不管是否发生异常,最终结果都要执行 finally语句
3、 在程序中可以使用 throws在方法的声明处声明,表示此方法不处理任何的异常。使用 throw人为的抛出一个异常。
4、 try、catch、finally、throw、throws联合使用才能形成一个异常的标准处理格式
5、 RuntimeException 的子类在程序中可以不用进行处理,如果有异常产生则交给 JVM 进行处理,当然,如果有需要也
可以加上 try…catch 进行处理。
6、 一个类只要继承了 Exception 类,那么此类就表示异常类。
7、 throwable分为两个子类
· Error:表示 JVM 出错,一般无法处理
· Exception:表示程序出错,一般由开发人员处理