当前位置:网站首页>@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
边栏推荐
- Docker compose deploy the flask project and build the redis service
- Win11系统小组件打不开?Win11系统小组件无法打开解决方法
- Page object and data driven test
- 细说GaussDB(DWS)复杂多样的资源负载管理手段
- MariaDB的安装与配置
- Lingyun going to sea | Wenhua online &huawei cloud: creating a new solution for smart teaching in Africa
- 74.股票的最大利润
- Selenium的各类API方法
- 学习放大器至少要3年?
- 高能直播,大咖云集!邀你共启BizDevOps探索之路。
猜你喜欢

Advanced features of selenium webdriver

【️爬虫必备->Scrapy框架从黑铁到王者️】初篇——万字博文详解(建议收藏)

k线图经典图解(收藏版)

开发者任务中心上线!千元豪礼送不停!

The developer task center is online! Thousands of yuan of gifts!

Inception 新结构 | 究竟卷积与Transformer如何结合才是最优的?

Rejected by a large company? Tencent experts summarized 11 reasons for being rejected!

4-2 port banner information acquisition

创作者基金会 6 月份亮点
![[software testing] 01 -- software life cycle and software development model](/img/57/a4fcb40a33c80b0bcf2187d1943abd.jpg)
[software testing] 01 -- software life cycle and software development model
随机推荐
STM32CubeMX 学习(6)外部中断实验
【历史上的今天】6 月 29 日:SGI 和 MIPS 合并;微软收购 PowerPoint 开发商;新闻集团出售 Myspace
unittest单元测试框架
Element waiting mechanism
Selenium WebDriver的高级特性
洞见科技作为「唯一」隐私计算数商,「首批」入驻长三角数据要素流通服务平台
Win11策略服务被禁用怎么办?Win11策略服务被禁用的解决方法
Intégration d'outils et de cadres tiers
NLP 类问题建模方案探索实践
Wechat launched the picture big bang function; Apple's self-developed 5g chip may have failed; Microsoft solves the bug that causes edge to stop responding | geek headlines
正则表达式系列之手机号码正则
Introduction to isalpha () method
Canonical的工程师们正努力解决Firefox Snap的性能问题
ovirt数据库修改删除节点
tp5 where查询数据库中的某个字段是否包含某个值,不用like的方法,模糊查询
75. nearest common ancestor of binary search tree
【笔记】再笔记--边干边学Verilog HDL – 014
75.二叉搜索树额最近公共祖先
开发者任务中心上线!千元豪礼送不停!
php实现 提取不重复的整数(编程题目能够最快的熟悉函数)
