【Java系列】:异常-Exception

☀️1 问题引入

运行下面的代码,看看有什么问题?

public static void main(String[] args) {
   
	int num1 = 10;
	int num2 = 0;
	int res = num1 / num2;
	System.out.println("程序继续运行....");
}

💻运行结果:

Exception in thread "main" java.lang.ArithmeticException: / by zero
	at com.hj.第十二章异常.a.main(a.java:21)

🧐解读:

  1. num1 / num2 => 10 / 0
  2. 当执行到 num1 / num2 因为 num2 = 0, 程序就会出现(抛出)异常 ArithmeticException
  3. 当抛出异常后,程序就退出,崩溃了 , 下面的代码就不在执行
  4. 大家想想这样的程序好吗? 不好,因为出现了一个不算致命的问题,就导致整个系统崩溃

☀️2 解决方案-异常捕获

java 设计者,提供了一个叫 异常处理机制来解决该问题,如果程序员,认为一段代码可能出现异常/问题,可以使用try-catch异常处理机制来解决从而保证程序的健壮性,将该代码块->选中->快捷键 ctrl + alt + t -> 选中 try-catch

public class a {
   
    public static void main(String[] args) {
   
        int num1 = 10;
        int num2 = 0;
        try {
   
            int res = num1 / num2;
        } catch (Exception e) {
   
            e.printStackTrace();
        }
        System.out.println("程序继续运行....");
    }
}

💡如果进行异常处理,那么即使出现了异常,程序可以继续执行
💻运行结果:

java.lang.ArithmeticException: / by zero
	at com.hj.第十二章异常.a.main(a.java:22)
程序继续运行....

可以看到运行结果中不仅提示了我们异常,还让程序(输出语句)继续执行下去。

如果想要知道异常的信息,可以使用下面代码:

try {
   
    int res = num1 / num2;
} catch (Exception e) {
   
    //e.printStackTrace();
    System.out.println("出现异常的原因:" + e.getMessage());//输出异常信息
}

☀️3 异常介绍

♗ 基本概念:
Java语言中,将程序执行中发生的不正常情况称为“异常”。(开发过程中的语法错误和逻辑错误不是异常)

执行过程中所发生的异常事件可分为两大类:

  1. Error(错误):Java虚拟机无法解决的严重问题。如:JVM系统内部错误、资源耗尽等严重情况。比如:StackOverflowError[栈溢出]和OOM(out of memory). Error是严重错误,程序会崩溃。
    2) Exception:其它因编程错误或偶然的外在因素导致的一般性问题,可以使用针对性的代码进行处理。例如空指针访问,试图读取不存在的文件,网络连接中断等等,Exception 分为两大类:运行时异常[程序运行时,发生的异常]和编译时异常[编程时,编译器检查出的异常]。

☀️4 异常体系图

在这里插入图片描述
🧐异常体系图的小结

  1. 异常分为两大类,运行时异常和编译时异常.
  2. 运行时异常,编译器检查不出来。一般是指编程时的逻辑错误,是程序员应该避免其出现的异常。java.lang.RuntimeException类及它的子类都是运行时异常
  3. 对于运行时异常,可以不作处理,因为这类异常很普遍,若全处理可能会对程序的可读性和运行效率产生影响
  4. 编译时异常,是编译器要求必须处置的异常。

☀️5 常见的运行时异常

🚀5.1 常见的运行时异常包括

  1. NullPointerException 空指针异常
  2. ArithmeticException 数学运算异常
  3. ArrayIndexOutOfBoundsException 数组下标越界异常
  4. ClassCastException 类型转换异常
  5. NumberFormatException 数字格式不正确异常

🚀5.2 常见的运行时异常举例

① NullPointerException 空指针异常
在这里插入图片描述
当应用程序试图在需要对象的地方使用 null 时,抛出该异常,看案例演示。

public class NullPointerException_ {
   
    public static void main(String[] args) {
   
        String name = null;
        System.out.println(name.length());
    }
}

💻运行结果:

Exception in thread "main" java.lang.NullPointerException
	at com.hj.第十二章异常.运行时异常.NullPointerException_.main(NullPointerException_.java:9)

② ArithmeticException 数学运算异常
在这里插入图片描述
当出现异常的运算条件时,抛出此异常。例如,一个整数“除以零”时,抛出此类的一个实例,案例如问题引出代码。

③ ArrayIndexOutOfBoundsException 数组下标越界异常
在这里插入图片描述
用非法索引访问数组时抛出的异常。如果索引为负或大于等于数组大小,则该索引为非法索引。

public class ArrayIndexOutOfBoundsException_ {
   
    public static void main(String[] args) {
   
        int[] arr = {
   1,2,4};
        for (int i = 0; i <= arr.length; i++) {
   
            System.out.println(arr[i]);
        }
    }
}

💻运行结果:

1
2
4
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
	at com.hj.第十二章异常.运行时异常.ArrayIndexOutOfBoundsException_.main(ArrayIndexOutOfBoundsException_.java:10)

④ ClassCastException 类型转换异常
在这里插入图片描述
当试图将对象强制转换为不是实例的子类时,抛出该异常。例如,以下代码将生成一个 ClassCastException

public class ClassCastException_ {
   
    public static void main(String[] args) {
   
        A b = new B(); //向上转型
        B b2 = (B)b;//向下转型,这里是 OK
        C c2 = (C)b;//这里抛出 ClassCastException
    }
}
class A {
   }
class B extends A {
   }
class C extends A {
   }
Exception in thread "main" java.lang.ClassCastException: com.hj.第十二章异常.运行时异常.B cannot be cast to com.hj.第十二章异常.运行时异常.C
	at com.hj.第十二章异常.运行时异常.ClassCastException_.main(ClassCastException_.java:10)

⑤NumberFormatException 数字格式不正确异常
在这里插入图片描述

当应用程序试图将字符串转换成一种数值类型,但该字符串不能转换为适当格式时,抛出该异常 => 使用异常我们可以确保输入是满足条件数字.

public class NumberFormatException_ {
   
    public static void main(String[] args) {
   
        String name = "hujian";
        //将 String 转成 int
        int num = Integer.parseInt(name);//抛出 NumberFormatException
        System.out.println(num);//1234
    }
}

💻运行结果:

Exception in thread "main" java.lang.NumberFormatException: For input string: "hujian"
	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
	at java.lang.Integer.parseInt(Integer.java:580)
	at java.lang.Integer
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值