1. 异常概述与异常体系结构
1.1 异常的概念及其分类
什么是异常
在使用计算机语言进行项目开发的过程中,即使程序员把代码写得尽善尽美,在系统的运行过程中仍然会遇到一些问题,因为很多问题不是靠代码能够避免的。
比如:客户输入数据的格式,读取文件是否存在,网络是否始终保持通畅等等。
异常:在Java语言中,将程序执行中发生的不正常情况称为“异常”。 (开发过程中的语法错误和逻辑错误不是异常)
异常的分类
异常事件可分为两类:
- Error:Java虚拟机无法解决的严重问题。 如:JVM系统内部错误、资源耗尽等严重情况。一般不编写针对性的代码进行处理。
- Exception: 其它因编程错误或偶然的外在因素导致的一般性问题。 可以使用针对性的代码进行处理。
1.2 Exception介绍
Exception的分类
- 编译时异常,也叫受检异常
- 运行时异常,非受检异常
1.3 Error介绍
一般栈溢出、堆溢出会造成Error
public class ErrorTest {
public static void main(String[] args) {
// 栈溢出:java.lang.StackoverflowError
main(args);
// 堆溢出:java.lang.OutOfMemoryError
int[] arr = new int[1024*1024*1024];
}
}
2. 常见异常
- 编译时异常:
- IOException
- ClassNotFoundException
- 运行时异常:
- NullPointerException:空指针异常
- ArrayIndexOutOfBoundsException:数组角标越界
- ClassCastException:类型转换异常
- NumberFormatException:数值转换异常
- InputMismatchException:输入不匹配异常
- ArithmeticException:算数异常
异常的处理方式
程序在正常执行的过程中,一旦出现异常,就会在异常代码处生成一个对应异常类的对象。并将此对象抛出。一旦抛出对象以后,其后的代码就不再执行。
有两种方式去解决异常:
- try-catch-finally
- throws
3. 异常处理机制一:try-catch-finally
try-catch-finally的使用
try{
//可能出现异常的代码
}catch(异常类型1 变量名1){
//处理异常的方式1
}catch(异常类型2 变量名2){
//处理异常的方式2
}catch(异常类型3 变量名3){
//处理异常的方式3
}
...
finally{
//一定会执行的代码
}
try-catch-finally的说明
- finally是可选的。
- 使用try将可能出现异常代码包装起来,在执行过程中,一旦出现异常,就会生成一个对应异常类的对象,根据此对象的类型,去catch中进行匹配。
- 一旦try中的异常对象匹配到某一个catch时,就进入catch中进行异常的处理。一旦处理完成,就跳出当前的try-catch结构(在没有写finally的情况)。继续执行其后的代码。
- catch中的异常类型如果没有子父类关系,则谁声明在上,谁声明在下无所谓。catch中的异常类型如果满足子父类关系,则要求子类一定声明在父类的上面,否则,报错。
- catch中的异常类型如果满足子父类关系,则要求子类一定声明在父类的上面。否则,报错。常用的异常对象处理的方式:④ String getMessage(),②printStackTrace()。
- 在try结构中声明的变量,再出了try结构以后,就不能再被调用。
- finally中声明的是一定会被执行的代码。即使catch中又出现异常、try中有return语句、catch中有return语句等情况。finally中的代码也一定会执行。
public class ExtcptionTest {
public static void main(String[] args) {
ExceptionMethod em = new ExceptionMethod();
em.Test1();
}
}
class ExceptionMethod{
void Test1(){
String str = "abc";
try{
int num = Integer.parseInt(str);
}catch(NumberFormatException e){
System.out.println("出现数值转换异常");
e.printStackTrace();
String s = e.getMessage();
}finally{
System.out.println("一定会被执行");
}
}
}
4. 异常处理机制二:throws
4.1 throws说明
throws执行过程
"throws +异常类型"写在方法的声明处。指明此方法执行时,可能会抛出的异常类型。
一旦当方法体执行时,出现异常,仍会在异常代码处生成一个异常类的对象,此对象满足throws后异常类型时,就会被抛出。
异常代码后续的代码,就不再执行!
方法重写的规则之一:子类重写的方法抛出的异常类型不大于父类被重写的方法抛出的异常类型。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class ExceptionTest2 {
public static void main(String[] args) {
ExceptionMethod2 em = new ExceptionMethod2();
try {
//最终异常出现在main中,选择try-catch处理
em.method2();
}catch (IOException e){
e.printStackTrace();
}
}
}
class ExceptionMethod2 {
void method2() throws IOException{
//因为方法二调用方法一,并且方法一使用throws,所以方法二也有异常
//此时方法二处理异常的方式也为向上抛throws
method1();
}
void method1() throws FileNotFoundException, IOException {
//在方法一中发现2种异常,向上抛
File file = new File("hello.txt");
FileInputStream fis = new FileInputStream(file);
int data = fis.read();
while(data != -1){
System.out.println((char)data);
data = fis.read();
}
fis.close();
}
}
4.2 两种处理机制的对比
体会二者的不同以及如何选择
try-catch-finally:真正将异常处理掉了
throws:只是将异常抛给了方法的调用者,并没有将异常真正的处理
如果父类中被重写的方法没有throws方式处理异常,则子类重写的方法也不能使用throws,意味着如果子类重写的方法中有异常,必须使用try-catch-finally方式处理。
执行的方法a中,先后又调用了另外的几个方法,这几个方法是递进关系执行的。我们建议这几个方法使用throw的方式进行处理。而执行的方法a可以考虑使用try-catch-finally方式进行处理。
5. 手动抛出异常:throw
异常对象的产生:throw
异常对象的处理:throws
public class ThrowTest {
public static void main(String[] args) {
Student s = new Student();
try {
s.regist(-1000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Student{
private int id;
public void regist(int id) throws Exception {
if(id > 0){
this.id = id;
}else{
//手动的去抛出异常
throw new Exception("您输入的数据非法");
}
}
}
6. 用户自定义类异常
如何自定义异常
- 继承于现有的异常结构:RuntimeException、Exception
- 提供全局常量:serialVersionUID
- 提供重载的构造器
- 异常定义好后就可以把它当作正常的异常来使用
public class MyException extends RuntimeException{
static final long serialVersionUID = -1564896165486465L;
MyException(){
}
MyException(String msg){
super(msg);
}
}