异常概念
异常指程序在运行过程中发生的不正常事件,它会打断指令的正常执行流程。
异常分类

Java程序运行过程中所发生的异常事件从严重性可分为两类
- 错误(Error):错误一般发生在JVM内部错误或资源耗尽等情况, 这一类异常事件无法恢复或不可能捕获,将导致应用程序中断。
public class Test01 {
public static void main(String[] args) {
/**
* 1、错误(Error):JVM系统内部错误或资源耗尽等严重情况,
* 属于JVM需要负担的责任这一类异常事件无法恢复或不可能捕获,将导致应用程序中断。
*/
//StackOverflowError:栈内存溢出的错误
method();
//OutOfMemoryError - 内存溢出的错误
ArrayList<byte[]> list = new ArrayList<>();
/*while(true){
byte[] bs = new byte[1024*1024];
list.add(bs);
}*/
}
public static void method(){
method();
}
}
-
异常(Exception):其它因编程错误或偶然的外在因素导致的一般性问题。这类异常得到恰当的处理时,程序有机会恢复至正常运行状况。
-
运行时异常(RuntimeException)非受检性异常:编译器不要求强制处置的异常。一般是指编程时的逻辑错误。是程序员应该积极避免其出现的,java.lang.RuntimeException 及它的子类都是非受检异常。
- 算术异常(除0溢出):java.lang.ArithmeticException
- 数组下标越界:java.lang.ArrayIndexOutOfBoundsException
- 错误的类型转换:java.lang.ClassCastException
- 空指针访问:java.lang.NullPointerException
public class Test02 { public static void main(String[] args) { /** * 运行时异常 RuntimeException - 非受检(unchecked)异常(): * 编译器不要求强制处置的异常。一般是指编程时的逻辑错误。是程序员应该积极避免其出现的异常 */ //ArithmeticException 算数异常 // System.out.println(10/0); //ArrayIndexOutOfBoundsException 数组下标越界异常 // int[] arr = new int[10]; // System.out.println(arr[100]); //ClassCastException 类型转换异常 Object obj = new String(); Integer integer = (Integer) obj; System.out.println(integer); //NullPointerException 空指针异常 // method(null); } public static void method(String str) { System.out.println(str.length()); } } -
受检(checked)异常 - 一般性异常:编译器要求必须处置的异常。指的是程序在运行时由于外界因素造成的一般性异常。
- 没有找到指定名称的类:java.lang.ClassNotFoundException
- 访问不存在的文件(文件路径错误):java.io.FileNotFoundException
- 操作文件时发生的异常:java.io.IOException
- 操作数据库时发生的异常:java.sql.SQLException
public static void main(String[] args) throws ParseException, ClassNotFoundException, InstantiationException, IllegalAccessException, IOException { /** * 受检(checked)异常 - 一般性异常:编译器要求必须处置的异常。 * 指的是程序在运行时由于外界因素造成的一般性异常。 */ //1.ParseException 解析异常 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // Date date = sdf.parse("2021年8月12日 10:56:01"); Date date = sdf.parse("2021-8-12 10:56:01"); System.out.println(date); //2.ClassNotFoundException 类未找到异常 //获取Student类字节码文件对象 Class<?> c = Class.forName("day19.com.dream.exception01.Student"); //通过字节码文件对象创建Student对象 Student stu = (Student) c.newInstance(); System.out.println(stu); //3.FileNotFoundException 文件未找到异常,IOException 输入输出异常 //创建文件对象 File file = new File("D:\\Out.txt"); //输入流 FileInputStream fis = new FileInputStream(file); System.out.println("文件长度:" + fis.available()); fis.close();//关流 }
-
Java异常处理机制
- Java程序在执行过程中如果出现异常,会自动生成一个异常类对象,该异常对象将被自动提交给JVM,这个过程称为抛出 throw 异常。
- 当 JVM 接收到异常对象时,会寻找能处理这一异常的代码并把当前异常对象交给其处理,这一过程称为捕获 catch 异常和处理异常。
- 如果 JVM 找不到可以捕获异常的代码,则运行时系统将终止,相应的Java程序也将退出。
异常处理方式
方式一:
try{
...... //可能产生异常的代码
}catch( ExceptionName1 e ){
...... //异常的处理代码
}catch( ExceptionName2 e ){
...... //异常的处理代码
} finally{
...... //无论如何都会执行的语句
}
说明:
1.try 代码段包含的是可能产生异常的代码
2.try 代码段后跟一个或多个catch代码段。(或跟一个finally代码块)
3.JDK1.7新特性,一个catch代码可以声明多个能处理的特定异常的类型,多个类型之间用”|”隔开
例如: catch( ExceptionName1 | ExceptionName2 e){
...... //异常的处理代码
}
4.当异常发生时,程序会中止当前的流程去执行相应的catch代码块。
5.写catch代码时,先捕获的异常的范围不能大于后捕获的异常的范围。大的异常要写在后面
6.finally中的代码无论是否发生异常都会执行。
方式二:throws 抛出异常类型,在定义一个方法的时候可以使用throws关键字声明,使用throws声明的方法表示此方法不处理异常,而交给方法的调用出进行处理。
1.本身的程序处理不了了,往上一层抛,由上一层去处理
2.定义一个方法的时候,通过这种方式来告知调用方,我这个方法有可能会发生异常的
public 返回值类型 方法名(参数列表) throws 异常类{
}
方式三:throw 抛出单个具体异常对象(常用于自定义异常)
throw new RuntimeException("程序出现了异常");
try…catch
public static void main(String[] args) {
/**
* 1.try{}...catch{} 处理多个异常的情况,多个异常的处理方式不一样
*/
//注意:先捕获的异常范围不能大于后捕获的异常范围
Scanner scan = new Scanner(System.in);
System.out.println("请输入第一个数字:");
int a = scan.nextInt();
System.out.println("请输入第二个数字:");
int b = scan.nextInt();
System.out.println("请输入类路径:");
String classPath = scan.next();
try {
System.out.println("111");
System.out.println(a/b);
System.out.println("222");
Class<?> c = Class.forName(classPath);
System.out.println(c);
}
catch (ArithmeticException e) {//捕获异常
System.out.println("处理算数异常");
} catch (ClassNotFoundException e) {//捕获异常
System.out.println("处理类未找到异常");
}
// catch (ArithmeticException | ClassNotFoundException e) {//捕获异常
// System.out.println("处理异常");
// }
finally {
scan.close();
}
}
throws
public static void main(String[] args) {
/**
* 2.throws - 抛出异常
*/
try {
method02();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//抛出异常,将异常交给调用方处理
private static void method02() throws ClassNotFoundException {
method01();
}
//抛出异常,将异常交给调用方处理
public static void method01() throws ClassNotFoundException{
Scanner scan = new Scanner(System.in);
System.out.println("请输入类路径:");
String classPath = scan.next();
Class<?> c = Class.forName(classPath);
System.out.println(c);
scan.close();
}
throw
//自定义异常类
public class MyException extends Exception{
@Override
public String toString() {
return "除数不能为0的异常";
}
}
//测试类
public class Test04 {
public static void main(String[] args) {
/**
* 3.throw - 手动抛异常
*/
Scanner scan = new Scanner(System.in);
System.out.println("请输入第一个数字:");
int a = scan.nextInt();
System.out.println("请输入第二个数字:");
int b = scan.nextInt();
try {
if(b == 0){
throw new MyException();//手动抛出异常
}
} catch (MyException e) {
System.out.println(e);
b = a;
}
System.out.println(a/b);
scan.close();
}
}
391

被折叠的 条评论
为什么被折叠?



