When to throw IllegalStateException vs IllegalArgumentException?

Let's start with the Javadocs: IllegalStateException Signals that a method has been invoked at an illegal or inappropriate time. In other words, the Java environment or Java application is not in an appropriate state for the requested operation. IllegalArgumentException Thrown to indicate that a method has been passed an illegal or inappropriate argument. The problem with the above

何时抛出IllegalStateException与IllegalArgumentException?

让我们从Javadocs开始: IllegalStateException异常 表示某个方法在非法或不适当的时间被调用。 换句话说,对于请求的操作,Java环境或Java应用程序不处于适当的状态。 抛出:IllegalArgumentException 抛出以表明某个方法已通过非法或不恰当的参数。 上述问题在于它们非常黑白。 考虑一个方法是解析调用者提供的文件的用例。 该文件存在,可读,并且格式正确。 但是,文件中的某些内容不符合业务规则。 在这种情

Guava Preconditions RuntimeExceptions handling

As I understood, we use Guava Preconditions to fail fast, before changing some objects states (a nice answer here from stackoverflow). And this is good. However it throws Runtime exceptions and this is not the favorite exceptions for the user of an application (500 errors and so on ...). So I need you to give me some help in design. I have an interface that declares many methods. Each metho

Guava Preconditions RuntimeExceptions处理

据我了解,我们使用Guava Preconditions在更改某些对象状态之前快速失败(这是一个很好的答案,来自于stackoverflow)。 这很好。 但是它会引发运行时异常,这不是应用程序用户最喜欢的异常(500错误等等)。 所以我需要你给我一些设计上的帮助。 我有一个声明了许多方法的接口。 每个方法都有必须控制的参数(例如:not null)。 所以在实现类中,我使用如下的指令: Preconditions.checkNotNull(fooObj); 但是,调用

Does XMLEventReader/XMLStreamReader throws IllegalArgumentException

If there is any illegal argument corresponding an attribute (say date) inside the tags in the xml string passed to XMLEventReader/XMLStreamReader, then does XMLEventReader/XMLStreamReader throws a IllegalArgumentException or just sets the date attribute to null ? If it doesn't throw any exception,in that case is there any way to make it throw a desired exception in case of IllegalArgument p

XMLEventReader / XMLStreamReader是否引发IllegalArgumentException

如果在传递给XMLEventReader / XMLStreamReader的xml字符串中的标签中存在与属性(比如说日期)相对应的非法参数,那么XMLEventReader / XMLStreamReader是否会抛出IllegalArgumentException,或者只是将date属性设置为null? 如果它没有抛出任何异常,在这种情况下,是否有任何方法可以在任何属性传递IllegalArgument的情况下抛出所需的异常?

Service layer: What to return if query argument is null?

Lets say we have a service method getById(Long id) which returns an entity based on its id. What would be the proper action to do in case id is null? Throw IllegalArgumentException? Throw NullPointerException? (guavas Preconditions.checkNotNull does this) return null? Since there can never be an entity with id == null, returning null does not seem that bad? I mean if the id does not ex

服务层:如果查询参数为null,返回什么?

假设我们有一个服务方法getById(Long id) ,它返回一个基于它的id的实体。 在id为null的情况下应该采取的适当措施是什么? 抛出IllegalArgumentException? 抛出NullPointerException? (番石榴Preconditions.checkNotNull这样做) 返回null? 既然不会有id == null的实体,返回null看起来并不好? 我的意思是如果id不存在,那么该方法无论如何都会返回null。 先决条件是很好的单线程,但在这种情况下抛出一个Null

Guava preconditions checkNull, checkArgument

I want to check preconditions on a base class so that I know subtypes will always use constructor arguments that are valid. Let's take as an example a constructor that: takes 2 or more parameters takes parameters of different types for one parameter, it performs multiple checks (eg String is not null and not empty) How would one best use the Guava preconditions approach in that case

番石榴前提条件checkNull,checkArgument

我想检查基类的先决条件,以便我知道子类型将始终使用有效的构造函数参数。 我们以构造函数为例说明: 需要2个或更多参数 采用不同类型的参数 对于一个参数,它执行多重检查(例如,字符串不为空且不为空) 在这种情况下,如何最好地使用番石榴前提条件? 在这样一个模拟例子中:(这是人为的!) protected AbstractException(String errorMessage, Throwable errorCause) { super(errorMessage, errorCause); ch

Automated IllegalArgumentException message?

I always check the arguments of public functions and throw exceptions when something's wrong. (For private helpers I use assertions). Like this: if( a < 0 || a >= b ) throw new IllegalArgumentException("'a' must be greater or equal to 0 and smaller than b "); But it always annoys me to write these error messages. The message seems redundant to me, as the message is ju

自动IllegalArgumentException消息?

我总是检查公共函数的参数,并在发生错误时抛出异常。 (对于私人帮手我使用断言)。 喜欢这个: if( a < 0 || a >= b ) throw new IllegalArgumentException("'a' must be greater or equal to 0 and smaller than b "); 但它总是让我烦恼,写下这些错误信息。 这个消息对我来说似乎是多余的,因为这个消息只是对声明的否定 a < 0 || a >= b 。 我经常会发生这样的情况:我用重构来重命名变

Comparable and (MyClass)null

Say I have public class MyClass implements Comparable<MyClass> { public int compareTo(MyClass mc) { //<implementation ommited>... } } The docs for Comparable say that "The natural ordering for a class C is said to be consistent with equals if and only if e1.compareTo(e2) == 0 has the same boolean value as e1.equals(e2) for every e1 and e2 of class C. N

Comparable和(MyClass)null

说我有 public class MyClass implements Comparable<MyClass> { public int compareTo(MyClass mc) { //<implementation ommited>... } } Comparable的文档说:“当且仅当e1.compareTo(e2)== 0与e1.equals(e2)具有相同的布尔值时,每个e1和e2。 请注意,null不是任何类的实例,即使e.equals(null)返回false ,e.compareTo(null)也应抛出NullPointerException 。 它说“ e.comp

Overriding Proper Java equals method

I am overriding the equals() method in a Java class and found a conundrum that I can't explain. The standard equals() contract states this: It is reflexive: for any reference value x, x.equals(x) should return true. It is symmetric: for any reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true. It is transitive: for any reference values x, y,

重写正确的Java等于方法

我重写了Java类中的equals()方法,发现了一个我无法解释的难题。 标准equals()合同规定了这一点: 它是自反的:对于任何参考值x,x.equals(x)应该返回true。 它是对称的:对于任何参考值x和y,当且仅当y.equals(x)返回true时,x.equals(y)才返回true。 它是可传递的:对于任何引用值x,y和z,如果x.equals(y)返回true,并且y.equals(z)返回true,则x.equals(z)应该返回true。 它是一致的:对于任何参考值x

compareTo triggered at first add call on TreeSet in Java 1.7

I have a TreeSet with elements, and according to: http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html#compareTo%28T%29 [ The natural ordering for a class C is said to be consistent with equals if and only if e1.compareTo(e2) == 0 has the same boolean value as e1.equals(e2) for every e1 and e2 of class C. Note that null is not an instance of any class, and e.compareTo(null) shou

首先在Java 1.7中的TreeSet上添加调用compareTo

我有一个TreeSet元素,并根据: http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html#compareTo%28T%29 [当且仅当e1.compareTo(e2)== 0与e1.equals(e2)具有相同的布尔值时,类C的自然顺序被认为与等于一致。对于类C的每个e1和e2 null不是任何类的实例, 即使e.equals(null)返回false , e.compareTo(null)也应抛出NullPointerException。 ] Element类具有非空安全的compareTo方法 我有以下代

Why is this not throwing a NullPointerException?

Nead clarification for following code: StringBuilder sample = new StringBuilder(); StringBuilder referToSample = sample; referToSample.append("B"); System.out.println(sample); This will print B so that proves sample and referToSample objects refer to the same memory reference. StringBuilder sample = new StringBuilder(); StringBuilder referToSample = sample; sample.append("A"); referToSample.ap

为什么这不会抛出NullPointerException?

需要澄清以下代码: StringBuilder sample = new StringBuilder(); StringBuilder referToSample = sample; referToSample.append("B"); System.out.println(sample); 这将打印B以便证明sample和referToSample对象引用相同的内存引用。 StringBuilder sample = new StringBuilder(); StringBuilder referToSample = sample; sample.append("A"); referToSample.append("B"); System.out.println(referToSample); 这将打印AB