沉梦听雨的编程指南 沉梦听雨的编程指南
首页
  • 基础篇
  • 集合篇
  • 并发篇
  • JVM
  • 新特性
  • 计算机网络
  • 操作系统
  • 数据结构与算法
  • 基础篇
  • MySql
  • Redis
  • 达梦数据库
  • Spring
  • SpringBoot
  • Mybatis
  • Shiro
  • 设计须知
  • UML画图
  • 权限校验
  • 设计模式
  • API网关
  • RPC
  • 消息队列
  • SpringCloud
  • 分布式事务
  • 云存储
  • 搜索引擎
  • 多媒体框架
  • 虚拟机
  • 开发工具篇
  • 工具库篇
  • 开发技巧篇
  • 工具类系列
  • 随笔
  • 前端环境搭建
  • HTML与CSS
  • JS学习
  • Vue3入门
  • Vue3进阶
  • 黑马Vue3
  • 脚手架搭建
  • 瑞吉外卖
  • 黑马点评
  • vue-blog
  • 沉梦接口开放平台
  • 用户中心
  • 聚合搜索平台
  • 仿12306项目
  • 壁纸小程序项目
  • RuoYi-Vue
  • 博客搭建
  • 网站收藏箱
  • 断墨寻径摘录
  • 费曼学习法
Github (opens new window)

沉梦听雨

时间是最好的浸渍剂,而沉淀是最好的提纯器🚀
首页
  • 基础篇
  • 集合篇
  • 并发篇
  • JVM
  • 新特性
  • 计算机网络
  • 操作系统
  • 数据结构与算法
  • 基础篇
  • MySql
  • Redis
  • 达梦数据库
  • Spring
  • SpringBoot
  • Mybatis
  • Shiro
  • 设计须知
  • UML画图
  • 权限校验
  • 设计模式
  • API网关
  • RPC
  • 消息队列
  • SpringCloud
  • 分布式事务
  • 云存储
  • 搜索引擎
  • 多媒体框架
  • 虚拟机
  • 开发工具篇
  • 工具库篇
  • 开发技巧篇
  • 工具类系列
  • 随笔
  • 前端环境搭建
  • HTML与CSS
  • JS学习
  • Vue3入门
  • Vue3进阶
  • 黑马Vue3
  • 脚手架搭建
  • 瑞吉外卖
  • 黑马点评
  • vue-blog
  • 沉梦接口开放平台
  • 用户中心
  • 聚合搜索平台
  • 仿12306项目
  • 壁纸小程序项目
  • RuoYi-Vue
  • 博客搭建
  • 网站收藏箱
  • 断墨寻径摘录
  • 费曼学习法
Github (opens new window)
  • 开发工具篇

  • 工具库篇

    • lombok工具库

      • lombok注解使用小结
      • Builder用法解析
      • 异常相关注解
    • EasyExcel小记

    • 定时任务相关

    • Hutool工具库

    • 极光推送学习
    • OkHttp学习
    • BigDecimal类详解
    • PdfBox学习
    • OCR功能实现
  • 开发技巧篇

  • 工具类系列

  • 随笔

  • 开发日常
  • 工具库篇
  • lombok工具库
沉梦听雨
2024-09-24

异常相关注解

# 异常相关注解

@SneakyThrows:

  • 用于在方法声明中使用。它的作用是告诉编译器,在方法中抛出受检异常时,不需要显式地在方法签名中声明或捕获该异常。
  • 将被注解的方法中的受检查异常(checked exception)转换为不受检查异常(unchecked exception)
  • 编译器中相当于是被 try catch 了。
  • 只会抛出一个异常。
  • 可使得代码更加简洁。
  • 在使用之前,请确保已经熟悉并理解所使用的注解的作用和影响。

总结:这个注解用在方法上,可以将方法中的代码用 try-catch 语句包裹起来,捕获异常并在 catch 中用 Lombok.sneakyThrow(e) 把异常抛出,可以使用 @SneakyThrows(Exception.class) 的形式指定抛出哪种异常。

示例 1: 使用 @SneakyThrows 注解抛出异常

public class SneakyThrowsExample {
    @SneakyThrows
    public void doSomething() {
        throw new Exception("An exception occurred.");
    }

    public static void main(String[] args) {
        SneakyThrowsExample example = new SneakyThrowsExample();
        example.doSomething();
    }
}
1
2
3
4
5
6
7
8
9
10
11

示例 2: @SneakyThrows 注解与方法签名中的异常声明结合使用

public class SneakyThrowsExample {
    @SneakyThrows(InterruptedException.class)
    public void doSomething() {
        Thread.sleep(1000);
    }

    public static void main(String[] args) {
        SneakyThrowsExample example = new SneakyThrowsExample();
        example.doSomething();
    }
}
1
2
3
4
5
6
7
8
9
10
11

示例 3: 在 lambda 表达式中使用 @SneakyThrows 注解

public class SneakyThrowsExample {
    public static void main(String[] args) {
        Runnable runnable = () -> {
            @SneakyThrows
            String message = getMessage();
            System.out.println(message);
        };

        Thread thread = new Thread(runnable);
        thread.start();
    }

    public static String getMessage() throws InterruptedException {
        Thread.sleep(1000);
        return "Hello, world!";
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

使用 @SneakyThrows 注解后同时遇到空指针异常和不合法参数异常的情况:

public class ExceptionExample {
    @SneakyThrows
    public static void main(String[] args) {
        String str = null;
        int length = str.length(); // NullPointerException

        int[] arr = new int[5];
        int value = arr[10]; // ArrayIndexOutOfBoundsException
    }
}

// 指定了异常也不会抛出两个异常
// @SneakyThrows({NullPointerException.class, ArrayIndexOutOfBoundsException.class})
1
2
3
4
5
6
7
8
9
10
11
12
13

控制台输出:

Exception in thread "main" java.lang.NullPointerException
	at com.chenmeng.project.controller.ExceptionExample.main(ExceptionExample.java:9)

进程已结束,退出代码1
1
2
3
4

结果分析:

  1. 使用 @SneakyThrows 注解后,在方法体中同时遇到空指针异常(NullPointerException)和不合法参数异常(ArrayIndexOutOfBoundsException)时,控制台只显示了空指针异常的信息,而不会显示不合法参数异常的信息。
  2. 这是因为 @SneakyThrows 注解会将异常转换为通用的 java.lang.Exception 类型,以通过编译。但是,由于只能抛出单个异常,因此只有第一个异常会被捕获和抛出,而后续的异常会被忽略。
  3. 即使指定了两个异常也只会抛出一个异常。

可阅读以下两篇文章:

  • @SneakyThrows注解 (opens new window)
  • @SneakyThrows注解的作用 (opens new window)
上次更新: 2024/9/25 11:16:13
Builder用法解析
工具类

← Builder用法解析 工具类→

Theme by Vdoing | Copyright © 2023-2025 沉梦听雨 | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式