当前位置:网站首页>@SneakyThrows注解
@SneakyThrows注解
2022-06-29 19:06:00 【沛沛老爹】
作用
@SneakyThrows就是使用该注解后不需要担心Exception的异常处理。
在说Exception之前,我们先简单聊下Error和Exception的差异
Error和Exception
Error是编译时错误和系统错误,系统错误在非特殊情况下,基本不会出现。而编译时错误,如果你使用了编译器,那么编译器会提示。
Exception则是可以被抛出的基本类型,我们需要主要关心的也是这个类。
Exception又可以根据运行时间来看,可以分为RunTimeException和其他Exception。
RunTimeException和其他Exception
其他Exception,受检查异常。可以理解为错误,必须要开发者解决以后才能编译通过,解决的方法有两种,
1:throw到上层,
2,try-catch处理。
RunTimeException:运行时异常,又称不受检查异常,不受检查,因为不受检查,所以在代码中可能会有RunTimeException时Java编译检查时不会告诉你有这个异常,但是在实际运行代码时则会暴露出来。如果不处理也会被Java处理。 例如报500异常。
@SneakyThrows的作用
现实大部分情况下的异常,我们都是一路往外抛了事。所以渐渐的java程序员处理Exception的常见手段就是外面包一层RuntimeException,接着往上丢
try{
}catch(Exception e){
throw new RuntimeException(e);
}
而Lombok的@SneakyThrows就是为了消除这样的模板代码。
使用注解后不需要担心Exception的处理
import lombok.SneakyThrows;
public class SneakyThrowsExample implements Runnable {
@SneakyThrows(UnsupportedEncodingException.class)
public String utf8ToString(byte[] bytes) {
return new String(bytes, "UTF-8");
}
@SneakyThrows
public void run() {
throw new Throwable();
}
}
起通过编译器生成真正的代码:
import lombok.Lombok;
public class SneakyThrowsExample implements Runnable {
public String utf8ToString(byte[] bytes) {
try {
return new String(bytes, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw Lombok.sneakyThrow(e);
}
}
public void run() {
try {
throw new Throwable();
} catch (Throwable t) {
throw Lombok.sneakyThrow(t);
}
}
}总结
总的来说,SneakyThrows注解就是减少了代码量,让代码看起来更加的整洁。
然后在编译的时候。Lombok会帮我们把try{}catch()再加上去。
参考:https://blog.csdn.net/qq_22162093/article/details/115486647
边栏推荐
- 【Proteus仿真】矩阵键盘中断扫描
- 福昕软件受邀亮相2022先进制造业数智发展论坛
- MBA-day19 如果p则q矛盾关系p 且非q
- PHP outputs all dates between two specified dates
- Introduction to isalpha () method
- jfinal中如何使用过滤器监控Druid监听SQL执行?
- 74.股票的最大利润
- The developer task center is online! Thousands of yuan of gifts!
- 数据库是什么?数据库详细笔记!带你走进数据库~你想知道的这里都有!
- 1. 使用STM32CubeMX建立STM32G030C8T6项目工程
猜你喜欢

2. add customized related files to the keil5 project established by stm32cubemx

3-2主机发现-三层发现

C#_摄像头图像转换为Bitmap格式及绘制十字线

Solution of portable emergency communication system for flood control and rescue

DAO 中存在的不足和优化方案

3-3 host discovery - layer 4 discovery

Machine learning 8-artificial neural network

tp5 where查询数据库中的某个字段是否包含某个值,不用like的方法,模糊查询

信息系统项目管理师---第七章项目成本管理历年考题

selenium的跨浏览器测试
随机推荐
3-3主机发现-四层发现
Intégration d'outils et de cadres tiers
Meta agent model can be migrated to resist attacks
Flutter 调用百度地图APP实现位置搜索、路线规划
Oracle11.2.0.4-Rac集群hang分析记录
Page Object与数据驱动测试
75. nearest common ancestor of binary search tree
NLP 类问题建模方案探索实践
Determination of concurrent number of pressure measurement
Third party tools and framework integration
Who took advantage of the chaos and looted in Tiktok Wenwan?
1. use stm32cubemx to establish stm32g030c8t6 project
3-3 host discovery - layer 4 discovery
Deep learning --- the weight of the three good students' scores (2)
AI场景存储优化:云知声超算平台基于 JuiceFS 的存储实践
Seven cattle image access parameters (thumbnails are small in size to avoid wasting traffic)
Machine learning 8-artificial neural network
揭秘!付费会员制下的那些小心机!
3-3主機發現-四層發現
MariaDB的安装与配置
