一、基本介绍
1、基本概念
java语言中,将程序执行中发生的不正常情况称为异常。(开发过程中的语法错误和逻辑错误不是异常)
2、执行过程中所发生的异常情况可分为两类
(1)Error(错误):java虚拟机无法解决的严重问题。如:JVM系统内部错误、资源耗尽等严重情况。
(2)Exception:其他因编程错误或偶然的外在因素导致的一般性问题,可以使用针对性的代码进行处理,例如空指针访问,试图读取不存在的文件等。
二、Exception分为两大类:
运行时异常(程序运行时发生的异常)和编译时异常(编程时,编译器检查出的异常)。
1、异常体系图一览
(还有许多异常并未列出。)
2、常见的运行时异常
运行时异常,编译器检查不出来。java.lang.RuntimeException类及它的子类都是运行时异常。
(1)NullPointerException空指针异常。
当运行程序试图在需要对象的地方使用null时,抛出NullPointerException。
public class C {
public static void main(String[] args) {
String name=null;
System.out.println(name.length());
}
}
运行结果:
(2)ArithmeticException数学运算异常。
当出现异常的运算条件时,抛出ArithmeticException。例如,一个整数“除以零”时。
public class A {
public static void main(String[] args) {
int a=10;
int b=0;
int c=a/b;
}
运行结果:
(3)ArrayIndexOutOfBoundsException数组下标越界异常。
用非法索引访问数组时抛出的ArrayIndexOutOfBoundsException,如果索引为负或>=数组大小,则该索引为非法索引。
public class D {
public static void main(String[] args) {
int []arr={1,2,4};
for(int i=0;i<=arr.length;i++) {
System.out.println(arr[i]);
//数组长度为3,所以i=3
}
}
}
运行结果:
(4)ClassCastException类型转换异常。
当试图将对象转换为不是实例的子类时,抛出ClassCastException。
(5)NumberFormatException数字格式不正确异常。
当应用程序试图将字符串转换成一种数值类型,但该字符串不能转换为适当格式时,抛出
NumberFormatException。
当字符串可以转换为适当格式时,如下:
public class DD {
public static void main(String[] args) {
String str="1234";
int a=Integer.parseInt(str);
System.out.println(a);
}
}
运行结果:
而当字符串不能转换为适当格式时,如下:
public class DD {
public static void main(String[] args) {
String str="hello";
int a=Integer.parseInt(str);
System.out.println(a);
}
}
运行结果:
2、常见的编译时异常。
编译器可以检查出来的并要求必须处理的异常。
如FileNotFoundException。当操作一个不存在的文件时发生的异常。
编译器在编译时就会提示存在该异常:
三、异常处理
以以下代码为例:
public class A {
public static void main(String[] args) {
int a=10;
int b=0;
int c=a/b;
System.out.println("程序继续运行......");
}
运行结果:
此时会发生算术异常,程序退出,后面的代码无法执行,影响整个程序的运行。为了不因一些不算致命的异常就导致程序奔溃,提供了异常处理机制。
异常处理方式
1、抛出异常——throws
将发生的异常抛出,交给调用者(方法)来处理,最顶级的处理者就是JVM。
(1)如果一个方法(中的语句执行时)可能生成某种异常,但不能确定如何处理这种异常,则此方法应该显示地抛出异常,表明该方法将不对这些异常进行处理,而由该方法的调用者负责处理。
public class FF {
public static void main(String[] args) {
}
public static void f()throws Exception{
FileInputStream fis=new FileInputStream("d://aa.txt");
}
}
(2)在方法声明中用throws语句可以声明抛出异常的列表,throws后面的异常类型可以是方法中产生的异常类型,也可以是它的父类。
public class FF {
public static void main(String[] args) {
}
public static void f() throws FileNotFoundException{
FileInputStream fis=new FileInputStream("d://aa.txt");
}
}
public class FF {
public static void main(String[] args) {
}
public static void f() throws FileNotFoundException,Exception{
FileInputStream fis=new FileInputStream("d://aa.txt");
}
}
2、捕获异常——try-catch-finally
在代码中捕获发生的异常,自行处理。
java提供try-catch来处理异常,try用于包含可能可能出错的代码。catch块用于处理try块中发生的异常。
语法格式:
try{
//代码可能有异常
}catch(Exception e){
//当异常发生时,系统将异常封装成Exception对象e,传递给catch。
//得到异常对象后,程序员自己出来
//注意:如果没有发生异常,catch代码块不执行
}finally{
//不管try代码块是否有异常发生,始终要执行finally
//也可不写finally,程序也可编译通过。
}
示例:处理上文中出现的算术异常
public class A {
public static void main(String[] args) {
int a=10;
int b=0;
try {
int c=a/b;
} catch (Exception e){
e.printStackTrace();
}
System.out.println("程序继续运行......");
}
运行结果:不影响后面代码的执行
注意事项:
(1)如果异常发生了,则异常发生后面的代码不会执行,直接进入到catch块。
(2)如果异常没有发生,则程序执行try块的代码,不会进入到catch。
(3)如果希望不管是否发生异常,都执行某段代码(如关闭连接,释放资源等),则需使用
-finally{}代码块。
(4)可以有多个catch语句,捕获不同的异常(进行不同的处理),要求父类异常在后,子类异常在前(如Exception在后,NullPointerException在前),如果发生异常,只会匹配一个catch。
示例:
只存在一个异常时:
public class EE {
public static void main(String[] args) {
try {
Person person=new Person("张三");
//person=null;
System.out.println(person.getName());
int a=10;
int b=0;
int res=a/b;
} catch (NullPointerException e) {
e.printStackTrace();
} catch (ArithmeticException e){
e.printStackTrace();
}catch (Exception e){
e.printStackTrace();
}finally {
System.out.println("ok");
}
}
}
class Person{
private String name;
public Person(){}
public Person(String name){
this.name=name;
}
public String getName() {
return name;
}
}
存在两个异常,只匹配一个catch: