日韩性视频-久久久蜜桃-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的全部內容,希望文章能夠幫你解決所遇到的問題。

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