文章目录
1.异常概述与异常体系结构
1.1背景
在使用计算机语言进行项目开发的过程中,即使程序员把代码写得尽善尽美,
在系统的运行过程中仍然会遇到一些问题,因为很多问题不是靠代码能够避
免的,比如:客户输入数据的格式,读取文件是否存在,网络是否始终保持 通畅
等等。
1.2异常
在Java语言中,将程序执行中发生的不正常情况称为“异常”。 (开发过程中的语法错误和逻辑错误不是异常)
1.2.1Java程序在执行过程中所发生的异常事件可分为两类:
1.2.1.1Error
Java虚拟机无法解决的严重问题。如:JVM系统内部错误、资源
耗尽等严重情况。比如:StackOverflowError和OOM。一般不编写针对性
的代码进行处理。
package com.my.java;
public class ErrorTest {
public static void main(String[] args) {
//1.栈溢出:java.lang.StackOverflowError
// main(args);
//2.堆溢出:java.lang.OutOfMemoryError: Java heap space
Integer[] arr = new Integer[1024*1024*1024];
}
}
1.2.1.2Exception
其它因编程错误或偶然的外在因素导致的一般性问题,可以使
用针对性的代码进行处理。例如:
- 空指针访问
- 试图读取不存在的文件
- 网络连接中断
- 数组角标越界
1.2.2解决方法
对于这些错误,一般有两种解决方法:一是遇到错误就终止程序的运行。另一种方法是由程序员在编写程序时,就考虑到错误的检测、错误消息的提示,以及错误的处理。
捕获错误最理想的是在编译期间,但有的错误只有在运行时才会发生。比如:除数为0,数组下标越界等
1.2.3分类:编译时异常和运行时异常
- java.lang.Throwable
1.1java.lang.Error:一般不编写针对性的代码进行处理。
1.2java.lang.Exception:可以进行异常的处理
- 编译时异常(checked)
IOException
FileNotFoundException
ClassNotFoundException
2. 运行时异常(unchecked,RuntimeException)
NullPointerException
ArrayIndexOutOfBoundsException
ClassCastException
NumberFormatException
InputMismatchException
ArithmeticException
更多的异常可以查看API
蓝色:运行时异常也可以说是非受检(unchecked)异常
红色:编译时异常也可以说是受检(checked)异常
1.2.4常见的异常类型例子
1.2.4.1运行时异常
1.2.4.1.1NullPointerException(空指针类型异常)
package com.my.java;
import org.junit.Test;
public class ExceptionText {
//NullPointerException
@Test
public void test1() {
// int[] arr = null;
// System.out.println(arr[3]);
String str = "abc";
str = null;
System.out.println(str.charAt(0));
}
}
1.2.4.1.2IndexOutOfBoundsException(索引越界异常)
- ArrayIndexOutOfBoundsException
- StringIndexOutOfBoundsException
package com.my.java;
import org.junit.Test;
public class ExceptionText {
//IndexOutOfBoundsException
@Test
public void test2() {
//ArrayIndexOutOfBoundsException
// int[] arr = new int[10];
// System.out.println(arr[10]);
//StringIndexOutOfBoundsException
String str = "abc";
System.out.println(str.charAt(3));
}
}
1.2.4.1.3ClassCastException(运行时异常)
package com.my.java;
import java.util.Date;
import org.junit.Test;
public class ExceptionText {
//ClassCastException
@Test
public void test3() {
Object obj = new Date();
String str = (String)obj;
}
}
1.2.4.1.4NumberFormatException(数值类型不匹配异常)
package com.my.java;
import org.junit.Test;
public class ExceptionText {
//NumberFormatException
@Test
public void test4() {
String str = "123";
str = "abc";
int num = Integer.parseInt(str);
}
}
1.2.4.1.5InputMismatchException(输入不匹配异常)
package com.my.java;
import java.util.Scanner;
import org.junit.Test;
public class ExceptionText {
//InputMismatchException
@Test
public void test5() {
Scanner scanner = new Scanner(System.in);
int score = scanner.nextInt();//当输入的不是数字而是字母的时候就会报这种错误
System.out.println(score);
scanner.close();
}
}
1.2.4.1.6ArithmeticException(除0异常)
package com.my.java;
import org.junit.Test;
public class ExceptionText {
//ArithmeticException
@Test
public void test6() {
int a = 10;
int b = 0;
System.out.println(a / b);
}
}
1.2.4.2编译时异常
1.2.4.2.1IOException、FileNotFoundException、ClassNotFoundException
package com.my.java;
import java.io.File;
import java.io.FileInputStream;
import org.junit.Test;
public class ExceptionText {
@Test
public void test7() {
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();
}
}
1.3异常处理:抓抛模型
1.3.1过程一
“抛”:程序在正常执行的过程中,一旦出现异常,就会在异常代码处生成一个对应异常类的对象。并将此对象抛出。一旦抛出对象以后,其后的代码就不再执行。
关于异常对象的产生:① 系统自动生成的异常对象
② 手动的生成一个异常对象,并抛出(throw)
1.3.2过程二
“抓”:可以理解为异常的处理方式:① try-catch-finally ② throws