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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java异常什么时候抛出异常,java - 什么时候应该抛出IllegalArgumentException?

發布時間:2024/10/8 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java异常什么时候抛出异常,java - 什么时候应该抛出IllegalArgumentException? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

將IllegalArgumentException視為先決條件檢查,并考慮設計原則:公共方法應該知道并公開記錄其自身的前提條件。

我同意這個例子是正確的:

void setPercentage(int pct) {

if( pct < 0 || pct > 100) {

throw new IllegalArgumentException("bad percent");

}

}

If EmailUtil is opaque, meaning there's some reason the preconditions cannot be described to the end-user, then a checked exception is correct. The second version, corrected for this design:

import com.someoneelse.EmailUtil;

public void scanEmail(String emailStr, InputStream mime) throws ParseException {

EmailAddress parsedAddress = EmailUtil.parseAddress(emailStr);

}

If EmailUtil is transparent, for instance maybe it's a private method owned by the class under question, IllegalArgumentException is correct if and only if its preconditions can be described in the function documentation. This is a correct version as well:

/** @param String email An email with an address in the form abc@xyz.com

* with no nested comments, periods or other nonsense.

*/

public String scanEmail(String email)

if (!addressIsProperlyFormatted(email)) {

throw new IllegalArgumentException("invalid address");

}

return parseEmail(emailAddr);

}

private String parseEmail(String emailS) {

// Assumes email is valid

boolean parsesJustFine = true;

// Parse logic

if (!parsesJustFine) {

// As a private method it is an internal error if address is improperly

// formatted. This is an internal error to the class implementation.

throw new AssertError("Internal error");

}

}

這種設計可以采用兩種方式。

If preconditions are expensive to describe, or if the class is intended to be used by clients who don't know whether their emails are valid, then use IllegalArgumentException. The top level method here is named scanEmail which hints the end user intends to send unstudied email through so this is likely correct.

If preconditions can be described in function documentation, and the class does not intent for invalid input and therefore programmer error is indicated, use IllegalArgumentException. Although not "checked" the "check" moves to the Javadoc documenting the function, which the client is expected to adhere to. IllegalArgumentException where the client can't tell their argument is illegal beforehand is wrong.

A note on IllegalStateException: This means "this object's internal state (private instance variables) is not able to perform this action." The end user cannot see private state so loosely speaking it takes precedence over IllegalArgumentException in the case where the client call has no way to know the object's state is inconsistent. I don't have a good explanation when it's preferred over checked exceptions, although things like initializing twice, or losing a database connection that isn't recovered, are examples.

總結

以上是生活随笔為你收集整理的java异常什么时候抛出异常,java - 什么时候应该抛出IllegalArgumentException?的全部內容,希望文章能夠幫你解決所遇到的問題。

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