关于java异常

了解异常

我们之前代码中也接触过某些异常,如数组下标越界

int[] arr = {1, 2, 3};
 System.out.println(arr[100]);
 // 执行结果
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 100

空指针异常

public class Test {
 public int num = 10;
 public static void main(String[] args) {
 Test t = null;
 System.out.println(t.num);
    }
 }
 // 执行结果
Exception in thread "main" java.lang.NullPointerException

异常指的就是程序在 运行时 出现错误时通知调用者的一种机制。

(1)有些错误如 System.out.println 拼写错了, 写成了 system.out.println. 此时编译过程中就会出错, 这是 “编译期” 出错。
(2)而运行时指的是程序已经编译通过得到 class 文件了, 再由 JVM 执行过程中出现的错误。

防御性异常

在程序出现问题的时候及时通知程序猿. 我们有两种主要的方式。
LBYL在操作之前就做充分的检查。
EAFP也就是先操作, 遇到问题再处理。

异常的优点

例如, 我们用伪代码演示一下开始一局王者荣耀的过程。
下面我们来对比一下。

(1)LBYL风格(不使用异常)

boolean ret = false;
 ret = 登陆游戏();
 if (!ret) {
处理登陆游戏错误;
 return;
 }
 ret = 开始匹配();
 if (!ret) {
处理匹配错误;
 return;
 }
 ret = 游戏确认();
 if (!ret) {
处理游戏确认错误;
 return;
 }
 ......

(2)EAFP 风格的代码(使用异常)

 try {
登陆游戏();
开始匹配();
游戏确认();
选择英雄();
载入游戏画面();
    ...
 } catch (登陆游戏异常) {
处理登陆游戏异常;
 } catch (开始匹配异常) {
处理开始匹配异常;
 } catch (游戏确认异常) {
处理游戏确认异常;
 } catch (选择英雄异常) {
处理选择英雄异常;
 } catch (载入游戏画面异常) {
处理载入游戏画面异常;
 }

异常语法

try{    
有可能出现异常的语句 ; 
}[catch (异常类型 异常对象) {
 } ... ]
 [finally {
异常的出口
}]

try 代码块中放的是可能出现异常的代码.
catch 代码块中放的是出现异常后的处理行为.
finally 代码块中的代码用于处理善后工作, 会在最后执行.
其中 catch 和 finally 都可以根据情况选择加或者不加

不处理异常

int[] arr = {1, 2, 3};
 System.out.println("before");
 System.out.println(arr[100]);
 System.out.println("after");
 // 执行结果
before
 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 100

我们发现,一旦出现异常,程序终止.

catch处理的异常

int[] arr = {1, 2, 3};
 try {
 System.out.println("before");
 arr = null;
 System.out.println(arr[100]);
 System.out.println("after");
 } catch (ArrayIndexOutOfBoundsException e) {
 e.printStackTrace();
 }
 System.out.println("after try catch");
 // 执行结果
before
 Exception in thread "main" java.lang.NullPointerException
 at demo02.Test.main(Test.java:11)

catch 语句不能捕获到刚才的空指针异常. 因为异常类型不匹配。记住catch可以有多个,但是异常同一时间只能执行一个.

关于finally

int[] arr = {1, 2, 3};
 try {
 System.out.println("before");
 arr = null;
 System.out.println(arr[100]);
 System.out.println("after");
 } catch (Exception e) {
 e.printStackTrace();
 } finally {
 System.out.println("finally code");
 }
 // 执行结果
before
 java.lang.NullPointerException
 at demo02.Test.main(Test.java:12)
 finally code

无论是否出现异常,finally 一定会执行.

抛出异常throw

 public static void main(String[] args) { 
    System.out.println(divide(10, 0)); 
} 
public static int divide(int x, int y) { 
    if (y == 0) { 
        throw new ArithmeticException("抛出除 0 异常"); 
    } 
    return x / y; 
} 
// 执行结果 
Exception in thread "main" java.lang.ArithmeticException: 抛出除 0 异常 
at demo02.Test.divide(Test.java:14) 
at demo02.Test.main(Test.java:9) 

throws

我们在处理异常的时候, 通常希望知道这段代码中究竟会出现哪些可能的异常.
我们可以使用 throws 关键字, 把可能抛出的异常显式的标注在方法定义的位置

 public static int divide(int x, int y) throws ArithmeticException { 
    if (y == 0) { 
        throw new ArithmeticException("抛出除 0 异常"); 
    } 
    return x / y; 
}

登陆账户习题

public class Main {
    private static String userName = "admin";
    private static String password = "123456";

    public static void main(String[] args) {
        try {
            login("admin", "123456");
        } catch (UserError userError) {
            userError.printStackTrace();
        } catch (PasswordError passwordError) {
            passwordError.printStackTrace();
        }
    }
    public static void login(String userName, String password) throws UserError,PasswordError {
        if (!Main.userName.equals(userName)) {
            throw new UserError("用户名错误");
        }
        if (!Main.password.equals(password)) {
            throw new PasswordError("密码错误");
        }
        System.out.println("登陆成功");
    }
}
class UserError extends Exception {
    public UserError(String message) {
        super(message);
    }
}
class PasswordError extends Exception {
    public PasswordError(String message) {
        super(message);
    }
}
  • 24
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小强在此

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

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

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

打赏作者

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

抵扣说明:

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

余额充值