当前位置:网站首页>@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
边栏推荐
- 洞见科技作为「唯一」隐私计算数商,「首批」入驻长三角数据要素流通服务平台
- 程序员值得收藏的几款代码截图美化工具播
- 【历史上的今天】6 月 29 日:SGI 和 MIPS 合并;微软收购 PowerPoint 开发商;新闻集团出售 Myspace
- PHP Laravel 使用 aws 负载均衡器的 ip 错误问题
- Exploration and practice of NLP problem modeling scheme
- Rejected by a large company? Tencent experts summarized 11 reasons for being rejected!
- Flutter 2.0 FocusScope. of(context). The requestfocus (focusnode()) does not take effect
- Cantata 9.5版本已正式通过SGS-TÜV认证,符合所有主要软件安全标准
- jfinal中如何使用过滤器监控Druid监听SQL执行?
- 构建增强现实移动应用程序的六款顶级工具
猜你喜欢

75.二叉搜索树额最近公共祖先

STM32CubeMX 学习(6)外部中断实验

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

TP5 where queries whether a field in the database contains a value. Fuzzy queries are performed without the like method

Element waiting mechanism

销量赶不上拿钱速度,威马赴港救急

SQL Server Backup and restore command operations

Seven cattle image access parameters (thumbnails are small in size to avoid wasting traffic)

C语言数组专题训练

Why is informatization ≠ digitalization? Finally someone made it clear
随机推荐
MBA-day26 数的概念与性质
Oracle11.2.0.4-Rac集群hang分析记录
ArrayList< Integer> Use = = to compare whether the values are equal, and -129=- 129 situation thinking
QC协议+华为FCP+三星AFC快充取电5V9V芯片FS2601应用
75. nearest common ancestor of binary search tree
Who took advantage of the chaos and looted in Tiktok Wenwan?
3-3 host discovery - layer 4 discovery
curl下载示例
Cross browser testing of selenium
Win11系统频繁断网怎么办?Win11网络不稳定的解决方法
74.股票的最大利润
乐鑫面试流程
AI scene Storage Optimization: yunzhisheng supercomputing platform storage practice based on juicefs
细说GaussDB(DWS)复杂多样的资源负载管理手段
出逃与进军,临期食品的「双面江湖」
Deep learning --- the weight of the three good students' scores (2)
Arm 全面计算解决方案重新定义视觉体验强力赋能移动游戏
QC protocol + Huawei fcp+ Samsung AFC fast charging 5v9v chip fs2601 application
Using protobuf to link MySQL in unrealeengine plug-in
Canonical的工程师们正努力解决Firefox Snap的性能问题
