当前位置:网站首页>@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
边栏推荐
- After CDN is added to the website, the Font Icon reports an error access control allow origin
- The sales volume could not catch up with the speed of taking money. Weima went to Hong Kong for emergency rescue
- 虎符限币种提现 用户曲线出金即亏损
- Machine learning 7-Support vector machine
- Element waiting mechanism
- JS judge whether the array key name exists
- 聊聊eureka的delta配置
- [笔记]再笔记--边干边学Verilog HDL –008
- 3-3 host discovery - layer 4 discovery
- Fastdfs
猜你喜欢
销量赶不上拿钱速度,威马赴港救急
tp5 where查询数据库中的某个字段是否包含某个值,不用like的方法,模糊查询
How to use the low code platform of the Internet of things for service management?
Deep learning --- the weight of the three good students' scores (2)
Various API methods of selenium
AI场景存储优化:云知声超算平台基于 JuiceFS 的存储实践
STM32CubeMX 学习(6)外部中断实验
做白银k线图有多重要?
为什么信息化 ≠ 数字化?终于有人讲明白了
求职大厂被拒?腾讯高手总结了11条被拒的原因!
随机推荐
Machine learning 7-Support vector machine
4-2 port banner information acquisition
产品-Axure9(英文版),中继器(Repeater)实现表格内容的增删查改(CRUD)
压测并发数的确定
建立自己的网站(12)
领先11%,华为云天筹AI求解器再度登顶国际权威榜单
layer.prompt
PHP实现二维数组按指定的键名排序的方法
jfinal中如何使用过滤器监控Druid监听SQL执行?
74. maximum profit on shares
打新债线上开户安全吗
从CIO到顾问:IT领导者的转型之路
Advanced features of selenium webdriver
k线图经典图解(收藏版)
MBA-day26 数的概念与性质
微信推出图片大爆炸功能;苹果自研 5G 芯片或已失败;微软解决导致 Edge 停止响应的 bug|极客头条
Why is informatization ≠ digitalization? Finally someone made it clear
Arm 全面计算解决方案重新定义视觉体验强力赋能移动游戏
Have you mastered all the testing methods of technology to ensure quality and software testing?
【️爬虫必备->Scrapy框架从黑铁到王者️】初篇——万字博文详解(建议收藏)