Java基础之初识异常

异常知识汇总一

异常更多的会在项目中遇到,目前只是做了个简单的汇总

异常理解图:
异常理解图

  1. 错误(Error):JVM系统内部错误或资源耗尽等严重情况。属于JVM需要负担的责任这一类异常事件无法恢复或不可能捕获,将导致应用程序中断
//StackOverflowError:栈内存溢出的错误
public static void main(String[] args) {
    method();
}
public static void method(){
    method();
}
//OutOfMemoryError - 内存溢出的错误
public static void main(String[] args) {
	ArrayList<byte[]> list = new ArrayList<>();	
    while(true){
        byte[] bs = new byte[1024*1024];
        list.add(bs);
    }
}
  1. 运行时异常 RuntimeException - 非受检(unchecked)异常():

    编译器不要求强制处置的异常。一般是指编程时的逻辑错误。是程序员应该积极避免其出现的异常

//ArithmeticException 算数异常
System.out.println(10/0);
//ArrayIndexOutOfBoundsException 数组下标越界异常
int[] is = new int[10];
System.out.println(is[100000]);
//ClassCastException 类型转换异常
Object obj = new String();
Integer integer = (Integer) obj;
System.out.println(integer);
//NullPointerException 空指针异常
public static void main(String[] args) {
    method(null);
}
public static void method(String str) {
    System.out.println(str.length());
}
  1. 受检(checked)异常 - 一般性异常:编译器要求必须处置的异常。

    指的是程序在运行时由于外界因素造成的一般性异常。
    注:所有的异常都是在程序运行中才出现的,受检异常是由于编译器自己执行了代码才能给我们报错,面试易考

//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);
//ClassNotFoundException 类未找到异常
//获取Student类字节码文件对象
Class<?> c = Class.forName("com.dream.exception01.Student");
//通过字节码文件对象创建Student对象
Student stu = (Student) c.newInstance();
System.out.println(stu);
//FileNotFoundException 文件未找到异常
//IOException 输入输出异常
//创建文件对象
File file = new File("C:\\Users\\hp\\Desktop\\test.txt");
//输入流
FileInputStream fis = new FileInputStream(file);
System.out.println("文件长度:" + fis.available());
fis.close();//关流

try…catch

try{
…可能发生异常的代码…
} catch (异常类型 e) {//捕获异常
…处理异常的代码
}finally{
…不管是否发生异常,都会执行…
}
注意:finally{}根据需求可写可不写

//处理多个异常的情况,多个异常的处理方式不一样
//注意:先捕获的异常范围不能大于后捕获的异常范围
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("处理类未找到异常");
}  finally {
    scan.close();
}
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 | ClassNotFoundException e) {//捕获异常
    System.out.println("处理异常");
}finally {
    scan.close();
}

throws - 抛出异常

public static void main(String[] args) {
    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 - 手动抛异常

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) {
    b = 1;
}
System.out.println(a/b);
scan.close();
//自定义异常类
public class MyException extends Exception{
	@Override
	public String toString() {
		return "除数不能为0的异常";
	};
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cat_lkh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值