2014-04-01 81 views
1

什麼是在java中正確的方法來處理方法參數/內部錯誤?Java錯誤處理,處理這個問題的正確方法是什麼?

所有這三個方法都做同樣的事情,第二個方法中的異常也有一個「引起」部分。

請不要在第三種方法中,每次我想退出時都必須重複返回。

考慮一下,在java中異常處理是非常昂貴的(我讀過這個地方)。

謝謝!

public static String method(String arg) { 
    block: { 
    if (arg == null) { 
     logger.error("arg is null"); 
     break block; 
    } 
    try { 
     ... 
     return "ok"; 
    } catch (Exception e) 
     logger.error("help!", e); 
    } 
    } 
    return "ko"; 
} 

public static String method(String arg) { 
    try { 
    if (arg == null) { 
     throw new Exception("arg is null"); 
    } 
    ... 
    return "ok"; 
    } catch (Exception e) { 
    logger.error("help!", e); 
    return "ko"; 
    } 
} 

public static String method(String arg) { 
    String result = "ko"; 
    if (arg == null) { 
    logger.error("arg is null"); 
    return result; 
    } 
    try { 
    .. 
    result = "ok"; 
    } catch(Exception e) { 
    logger.error("help!", e); 
    } 
    return result; 
} 

編輯: 此外,在第二種方法,可以使用的RuntimeException(或者自定義),壞主意區分內部方法錯誤?

+0

拋出一個異常並用相同的方法捕捉它是沒有意義的,只有幾行。否則,第一和第三方案之間的選擇是個人偏好(以及其他設計壓力)之一。 –

+0

我會讓方法拋出異常 – Leo

回答

-1

你應該使用最容易閱讀的。請記住,代碼被寫入一次並讀取很多次。

第三個是最容易閱讀的。

另一個很好的規則。每種方法都有一個條目。 最好只使用一個return語句。

的原因是,它更容易在未來讀者瞭解思想與代碼什麼。

更多關於這類型的問題,可以在清潔守則瞭解http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882

建議對於一個入門存在的解決方案:

public static String method(String arg) { 
    String outString = "ko"; 
    try { 
     if (arg != null) { 
      outString = "ok"; 
     } else { 
      logger.debug("arg is null"); 
     } 
    } catch (Exception e) { 
     logger.error("Help!", e); 
    } 
    return outString; 
} 
+0

所以你告訴我,雖然第三個更容易閱讀,但第一個完成「一個入口/一個出口」規則比它是更好的方式要遵守? – Seby

+0

他們中的任何一個都遵循一個存在的規則 - 規則。 我會有這樣的代碼。 我試圖在評論中添加代碼。但不能。所以我用新的例子更新了我的迴應。 – Viggo

+0

你錯過了你的其他條款,因爲它會繼續執行neverless。 – Seby

0

彌補了第一個,因爲它是更優雅,但第三個是更清晰的閱讀和不容易出錯的恕我直言。

+0

這只是基於意見。除此之外,不惜一切代價避免標籤;它們主要被包含來支持自動代碼生成。 –

1

我不認爲「正確」的方式是任何這些3.

Java有一個例外,只爲無效的參數,稱爲IllegalArgumentException,這實際上是一個RuntimeException,所以你不聲明。這個想法是,如果你提供了一個非法的參數,這是調用者方面的一個錯誤,所以調用者必須捕獲並處理異常。

當你的方法返回一個有效的結果爲「非法」的說法,國際海事組織,你的說法是不是真的違法的,所以不應該有一個例外,然後沒有什麼歇着。所以,你的代碼應該看起來像

public static String method(String arg) { 
     return arg==null?"ko":"ok"; 
    } 

這裏沒有例外。

現在,如果一個空的說法是一些特殊情況,你必須要處理,我認爲正確的做法是把它在發送方。在JDK,你會發現這兩個顯性和隱性參數無效例外的例子,例如:

明確

* @param  s the string to be parsed. 
* @return  a {@code Double} object holding the value 
*    represented by the {@code String} argument. 
* @throws  NumberFormatException if the string does not contain a 
*    parsable number. 
*/ 
public static Double valueOf(String s) throws NumberFormatException { 
    return new Double(FloatingDecimal.readJavaFormatString(s).doubleValue()); 
} 

* @param uri 
*   the URI to convert 
* 
* @return the resulting {@code Path} 
* 
* @throws IllegalArgumentException 
*   if preconditions on the {@code uri} parameter do not hold. The 
*   format of the URI is provider specific. 
    (...) 
*/ 
public static Path get(URI uri) { 
    String scheme = uri.getScheme(); 
    if (scheme == null) 
     throw new IllegalArgumentException("Missing scheme"); 
    (...) 

我認爲這裏的整體思路是

  1. 如果null參數是一種例外情況,而不是您的方法所期望的,那麼這是一個錯誤,您必須記錄從被調用的方法之外的版本。如果被調用的方法可以處理null arg(例如返回一些有效值,例如「ko」),那麼它不是一個特殊情況,而只是一個有效結果,因此在這裏不需要例外。
  2. 否則,必須拋出一個異常(顯式或沒有),讓來電處理它

這就是我的想法。

相關問題