日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java代码中常见技术债务处理之Exception

發布時間:2025/3/19 java 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java代码中常见技术债务处理之Exception 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

寫在前面

異常處理是代碼中常見的處理,本文根據SonarQube在異常方面的規則和常見檢查結果,選取說明了常見異常處理中的技術債務,提倡技術債務最少的編碼方式。

Exception handlers should preserve the original exceptions

Either log or rethrow this exception.

When handling a caught exception, the original exception’s message and stack trace should be logged or passed forward.
NONCOMPLIANT CODE EXAMPLE

// Noncompliant - exception is lost try { /* ... */ } catch (Exception e) { LOGGER.info("context"); } // Noncompliant - exception is lost (only message is preserved) try { /* ... */ } catch (Exception e) { LOGGER.info(e.getMessage()); } // Noncompliant - exception is lost try { /* ... */ } catch (Exception e) { throw new RuntimeException("context"); }

COMPLIANT SOLUTION

try { /* ... */ } catch (Exception e) { LOGGER.info(e); } try { /* ... */ } catch (Exception e) { throw new RuntimeException(e); }try {/* ... */ } catch (RuntimeException e) {doSomething();throw e; } catch (Exception e) {// Conversion into unchecked exception is also allowedthrow new RuntimeException(e); }

錯誤實例:

protected int analyzeJobStep1(String jobName) {try {notifyBHandler();return analyzeJobStep2(jobName)} catch (Exception e) {logger.error(e.getMessage();}return 0;}解決實例:protected int analyzeJobStep1(String jobName) {int nRet=0;try {notifyBHandler();nRet = analyzeJobStep2(jobName);} catch (Exception e) {logger.error("notifyBHandler trigger exception", e);}return nRet;}

Don’t directly use Exception and RuntimeException

Sonarrule:Generic exceptions should never be thrown (squid:S00112)
Using such generic exceptions as Error, RuntimeException, Throwable, and Exception prevents calling methods from handling true, system-generated exceptions differently than application-generated errors.
Noncompliant Code Example

public void foo(String bar) throws Throwable { // Noncompliantthrow new RuntimeException("My Message"); // Noncompliant }

Compliant Solution

public void foo(String bar) {throw new MyOwnRuntimeException("My Message"); }

Another related rule: Generic exceptions should never be thrown
the below should be avoided.

@Override public void myMethod() throws Exception {...}

Define and throw a dedicated exception instead of using a generic one.

Exceptions should not be thrown in finally blocks

try {/* some work which end up throwing an exception */throw new IllegalArgumentException(); } finally {/* clean up */throw new RuntimeException(); // Noncompliant; will mask the IllegalArgumentException }

Compliant Solution

try {/* some work which end up throwing an exception */throw new IllegalArgumentException(); } finally {/* clean up */ // Compliant }

Checked exceptions should not be thrown

The purpose of checked exceptions is to ensure that errors will be dealt with, either by propagating them or by handling them, but some believe that checked exceptions negatively impact the readability of source code, by spreading this error handling/propagation logic everywhere.
This rule verifies that no method throws a new checked exception.

CODE EXAMPLE

public void myMethod1() throws CheckedException {...throw new CheckedException(message); // Noncompliant...throw new IllegalArgumentException(message); // Compliant; IllegalArgumentException is unchecked }

Solution Example

public void myMethod2() throws CheckedException { // Compliant; propagation allowedmyMethod1(); }

Public methods should throw at most one checked exception

Using checked exceptions forces method callers to deal with errors, either by propagating them or by handling them. Throwing exceptions makes them fully part of the API of the method.
But to keep the complexity for callers reasonable, methods should not throw more than one kind of checked exception.

NONCOMPLIANT CODE EXAMPLE

public void delete() throws IOException, SQLException { // Noncompliant/* ... */ }

COMPLIANT SOLUTION

public void delete() throws SomeApplicationLevelException {/* ... */ }

總結

以上是生活随笔為你收集整理的Java代码中常见技术债务处理之Exception的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 97在线播放免费观看 | 91精品婷婷国产综合久久 | 国产欧美一区二区在线 | 亚洲成人手机在线 | 久草视频在线免费看 | 国产日产亚洲系列最新 | 日本一区二区视频在线 | 91伊人网| 中文字幕丰满人伦在线 | 91在线精品秘密一区二区 | 少妇15p | 欧美成人三级精品 | 国产在线999| 性感美女一级片 | 日本aaa视频 | av成人在线免费观看 | 色哟哟一区二区 | 黄色在线观看网址 | 青青视频在线免费观看 | 久久中文视频 | 告诉我真相俄剧在线观看 | 五月天天 | 日韩大片免费观看 | 伊人久久久久久久久久久久久 | 51免费看成人啪啪片 | 影音先锋中文字幕在线播放 | 91成人国产综合久久精品 | 亚洲av无码乱码国产精品fc2 | 成人一区二区三区四区 | 专干老肥女人88av | 那里可以看毛片 | 狼人精品一区二区三区在线 | 国产高清视频在线观看 | 亚洲一区 中文字幕 | 最新免费黄色网址 | 蜜臀人妻四季av一区二区不卡 | 亚洲一区二区高清视频 | 亚洲成人网在线播放 | 伊人成综合 | 福利在线电影 | 黄色小视频国产 | 黄色成人一级片 | 国产在线一区二 | 免费99视频 | 小视频免费在线观看 | 91亚洲视频 | 久久久久网站 | 午夜tv | 国产精品国产精品国产专区不片 | 黄色avav| 一级一片免费播放 | 91全免费| 日韩三级中文字幕 | 色综合天天综合 | 免费亚洲网站 | 亚洲一区免费 | 99久久国产视频 | 日本猛少妇色xxxxx猛叫 | 日韩av手机在线播放 | 亚洲国产极品 | 三级成人网 | 99久久精品一区二区 | 天天cao| 免费看黄网站在线 | 麻豆精品视频 | 欧美黄色aaa| 少妇大叫太粗太大爽一区二区 | 浮力影院国产第一页 | av片网| 自拍偷拍福利 | 国产情侣酒店自拍 | 在线视频观看免费 | 魔女鞋交玉足榨精调教 | 色屁屁一区二区三区视频 | 国产精品情侣呻吟对白视频 | 亚洲日本欧美 | 国产91嫩草 | 国产性生活片 | 国产日批| 国产精品一区二区三区免费看 | 麻豆91茄子在线观看 | 成人毛片一区二区三区 | 午夜美女视频 | 草色网 | 国产在线一区二 | 久久黄色视 | 先锋影音一区二区 | 国产一区二区三区色淫影院 | 国产21页 | 国产精品一区二区久久毛片 | 人人妻人人澡人人爽人人欧美一区 | 欧美日韩视频无码一区二区三 | 一区二区三区在线播放 | 国产精品亚洲二区在线观看 | 少妇激情四射 | 草草影院网址 | 国产精品丝袜在线 | 超碰人人人人人人 | 少妇精品无码一区二区三区 |