一、异常概述
Exception 类是 Throwable 类的子类。它是因编程错误或由于偶然的外在因素导致的一般性问题,可以使用针对性的代码进行处理。
除了 Exception 类外,Throwable 还有一个子类 Error 。
Error 是指运行时环境发生的错误,Java 虚拟机无法解决的严重问题 。
例如,JVM 内存溢出。一般地,程序不会从错误中恢复, 一般不编写针对性
的代码进行处理。
异常类有两个主要的子类:IOException 类和 RuntimeException 类。
二、编译时异常
编译时异常也称为受检异常
[Java] 纯文本查看 复制代码 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] 纯文本查看 复制代码 java.io.FileNotFoundException: test.txt (系统找不到指定的文件。)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileReader.<init>(FileReader.java:72)
at com.zxy.java.exception.ExceptionTest1.main(ExceptionTest1.java:21)
三、运行时异常
运行时异常也被称作非受检异常
1.空指针异常
[Java] 纯文本查看 复制代码 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));
}
}
测试结果:
[Java] 纯文本查看 复制代码 Exception in thread "main" java.lang.NullPointerException
at com.zxy.java.exception.ExceptionTest2.main(ExceptionTest2.java:13)
2. 数组下标越界异常
[Java] 纯文本查看 复制代码 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]);
}
}
测试结果:
[Java] 纯文本查看 复制代码 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
at com.zxy.java.exception.ExceptionTest3.main(ExceptionTest3.java:12)
3. 数字格式异常
[Java] 纯文本查看 复制代码 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);
}
}
测试结果:
[Java] 纯文本查看 复制代码 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. 类型转换异常
[Java] 纯文本查看 复制代码 package com.zxy.java.exception;
import java.util.Date;
/**
* @Description: ClassCastException
* @Author: zhangxy
* @Date: Created in 2019/11/20
* @Modified By:
*/
public class ExceptionTest5 {
public static void main(String[] args) {
Object obj = new Date();
String str = (String) obj;
}
}
测试结果:
[Java] 纯文本查看 复制代码 Exception in thread "main" java.lang.ClassCastException: java.util.Date cannot be cast to java.lang.String
at com.zxy.java.exception.ExceptionTest5.main(ExceptionTest5.java:14)
5. 算术异常
[Java] 纯文本查看 复制代码 package com.zxy.java.exception;
import java.util.Date;
/**
* @Description: ArithmeticException
* @Author: zhangxy
* @Date: Created in 2019/11/20
* @Modified By:
*/
public class ExceptionTest6 {
public static void main(String[] args) {
int m = 8;
int n = 0;
System.out.println(m / n);
}
}
测试结果:
[Java] 纯文本查看 复制代码 Exception in thread "main" java.lang.ArithmeticException: / by zero
at com.zxy.java.exception.ExceptionTest6.main(ExceptionTest6.java:15)
6. 输入不匹配异常
[Java] 纯文本查看 复制代码 package com.zxy.java.exception;
import java.util.Scanner;
/**
* @Description: InputMismatchException
* @Author: zhangxy
* @Date: Created in 2019/11/20
* @Modified By:
*/
public class ExceptionTest7 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int wage = scanner.nextInt();
System.out.println(wage);
}
}
测试结果:
[Java] 纯文本查看 复制代码 aaa
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at com.zxy.java.exception.ExceptionTest7.main(ExceptionTest7.java:14)
|