当前位置:网站首页>@Sneakythlows annotation
@Sneakythlows annotation
2022-06-29 19:16:00 【Peipei's father】
effect
@SneakyThrows Just don't worry after using this annotation Exception Exception handling .
Say Exception Before , Let's have a brief talk first Error and Exception The difference of
Error and Exception
Error Are compile time errors and system errors , System error in non special cases , Basically there will be no . And compile time error , If you use a compiler , So the compiler will prompt .
Exception Is the basic type that can be thrown , This is also the class that we need to focus on .
Exception It can also be seen from the running time , Can be divided into RunTimeException And others Exception.
RunTimeException And others Exception
other Exception, Abnormal under examination . It can be understood as a mistake , It must be solved by the developer before it can be compiled , There are two solutions ,
1:throw To the top ,
2,try-catch Handle .
RunTimeException: Runtime exception , Also known as unchecked exception , No inspection , Because it is not inspected , So there may be in the code RunTimeException when Java The compilation check does not tell you that there is an exception , But it will be exposed when the code is actually running . If it is not handled, it will be Java Handle . For example, newspaper 500 abnormal .
@SneakyThrows The role of
In most cases, the reality is abnormal , We're all going out of the way . So gradually java Programmers deal with Exception A common way to do this is to put a layer on the outside RuntimeException, Then throw it up
try{
}catch(Exception e){
throw new RuntimeException(e);
}
and Lombok Of @SneakyThrows This is to eliminate such template code .
There is no need to worry after using annotations Exception To deal with
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();
}
}
To generate real code through the compiler :
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);
}
}
}summary
in general ,SneakyThrows Annotations reduce the amount of code , Make the code look cleaner .
Then when compiling .Lombok Will help us to try{}catch() Plus .
Reference resources :https://blog.csdn.net/qq_22162093/article/details/115486647
边栏推荐
- 从CIO到顾问:IT领导者的转型之路
- 云上未来,数智导航:阿里云研究院报告合集
- Who took advantage of the chaos and looted in Tiktok Wenwan?
- 程序员值得收藏的几款代码截图美化工具播
- 移动端测试
- 微信推出图片大爆炸功能;苹果自研 5G 芯片或已失败;微软解决导致 Edge 停止响应的 bug|极客头条
- 炒股用同花顺开户交易安全吗?
- 3-3 host discovery - layer 4 discovery
- 76. nearest common ancestor of binary tree
- How to install and use computer SSD hard disk
猜你喜欢

揭秘!付费会员制下的那些小心机!

深度好文 | YOLOv5+DeepSORT多目标跟踪深入解读与测试(含源码)

3-3 host discovery - layer 4 discovery

防汛救援便携式应急通信系统解决方案

@SneakyThrows注解

SQL Server Backup and restore command operations

Win11系统频繁断网怎么办?Win11网络不稳定的解决方法

3 - 3 découverte de l'hôte - découverte à quatre niveaux

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

做白银k线图有多重要?
随机推荐
逻辑结构与物理结构
程序员值得收藏的几款代码截图美化工具播
自动获取本地连接及网络地址修改
PHP implementation of sorting two-dimensional arrays by specified key names
Intégration d'outils et de cadres tiers
Inception 新结构 | 究竟卷积与Transformer如何结合才是最优的?
jfinal中如何使用过滤器监控Druid监听SQL执行?
selenium的跨浏览器测试
数据安全解决方案的大时代
高能直播,大咖云集!邀你共启BizDevOps探索之路。
isalpha()方法介绍
创作者基金会 6 月份亮点
AI场景存储优化:云知声超算平台基于 JuiceFS 的存储实践
Qui vole dans un jeu d'écriture?
startService() 过程
Sophomore majoring in software engineering, the previous learning situation is not very good. How to plan the follow-up development route
软件工程专业大二,之前的学习情况不太好该怎么规划后续发展路线
The sales volume could not catch up with the speed of taking money. Weima went to Hong Kong for emergency rescue
数据基础设施升级窗口下,AI 新引擎的技术方法论
AI场景存储优化:云知声超算平台基于 JuiceFS 的存储实践
