用知识改变命运,让我们的家人过上更好的生活
。
目录
-
- 一、异常概述
-
二、编译时异常
-
三、运行时异常
-
-
- 1.空指针异常
-
-
2. 数组下标越界异常
-
3. 数字格式异常
-
4. 类型转换异常
-
5. 算术异常
-
6. 输入不匹配异常
Exception 类是 Throwable 类的子类。它是因编程错误或由于偶然的外在因素导致的一般性问题,可以使用针对性的代码进行处理。
除了 Exception 类外,Throwable 还有一个子类 Error 。
Error 是指运行时环境发生的错误,Java 虚拟机无法解决的严重问题 。
例如,JVM 内存溢出。一般地,程序不会从错误中恢复, 一般不编写针对性
的代码进行处理。
异常类有两个主要的子类:IOException 类和 RuntimeException 类。
(此图片来源于网络)
编译时异常也称为受检异常
是指编译器要求必须进行处理的异常。即程序在运行时由于外界因素造成的一般性异常。
package com.zxy.java.exception;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
/**
-
@Description: FileNotFoundException
-
@Author: zhangxy
-
@Date: Created in 2019/11/20
-
@Modified By:
*/
public class ExceptionTest1 {
public static void main(String[] args) {
FileReader fileReader = null;
try {
// 实例化File对象
File file = new File(“test.txt”);
// 实例化FileReader流,用于数据的读入
fileReader = new FileReader(file);
// 读入数据
char[] chars = new char[5];
int length;
while ((length = fileReader.read(chars)) != -1) {
String s = new String(chars, 0, length);
System.out.println(s);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 不再使用的时候,关闭流资源
if (fileReader != null) {
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
测试结果:
java.io.FileNotFoundException: test.txt (系统找不到指定的文件。)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.(FileInputStream.java:138)
at java.io.FileReader.(FileReader.java:72)
at com.zxy.java.exception.ExceptionTest1.main(ExceptionTest1.java:21)
运行时异常也被称作非受检异常
是指编译器不要求必须处理的异常。一般是指编程时的逻辑错误。
1.空指针异常
当程序运行时,对象未初始化或为空时,将会抛出空指针异常。
package com.zxy.java.exception;
/**
-
@Description: NullPointerException
-
@Author: zhangxy
-
@Date: Created in 2019/11/20
-
@Modified By:
*/
public class ExceptionTest2 {
public static void main(String[] args) {
String s = “HelloWorld”;
s = null;
System.out.println(s.charAt(1));
}
}
测试结果:
Exception in thread “main” java.lang.NullPointerException
at com.zxy.java.exception.ExceptionTest2.main(ExceptionTest2.java:13)
2. 数组下标越界异常
指使用非法索引访问数组。索引为负值或大于或等于数组的大小。将会抛出数组下标越界异常。
package com.zxy.java.exception;
/**
-
@Description: ArrayIndexOutOfBoundsException
-
@Author: zhangxy
-
@Date: Created in 2019/11/20
-
@Modified By:
*/
public class ExceptionTest3 {
public static void main(String[] args) {
int[] array = new int[5];
System.out.println(array[6]);
}
}
测试结果:
Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 6
at com.zxy.java.exception.ExceptionTest3.main(ExceptionTest3.java:12)
3. 数字格式异常
一般由 Integer.valueOf(String param) 或者 Integer.parseInt(String param) 引起数字异常
package com.zxy.java.exception;
/**
-
@Description: NumberFormatException
-
@Author: zhangxy
-
@Date: Created in 2019/11/20
-
@Modified By:
*/
public class ExceptionTest4 {
public static void main(String[] args) {
String str = “111”;
str = “abc”;
int num = Integer.parseInt(str);
System.out.println(num);
}
}
Exception in thread “main” java.lang.NumberFormatException: For input string: “abc”
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at com.zxy.java.exception.ExceptionTest4.main(ExceptionTest4.java:13)
4. 类型转换异常
当程序试图将对象强制转换为不是该实例的子类时,将会抛出类型转换异常
package com.zxy.java.exception;