[ACCEPTED]-What are the most commonly used runtime exceptions in java?-runtime
I never throw NullPointerException. For me, it is one that appears 27 naturally in the code when something goes 26 wrong and that requires a developer to look 25 at what happens. Then (s)he fixes the cause 24 and it doesn't happen again.
I use IllegalStateException to signal 23 that an object is incorrectly configured 22 or that calls are in an incorrect order. However, we 21 all know that ideally, an object should 20 ensure it can't be in a bad state and that 19 you can't call it in incorrect order (make 18 a builder and a resulting object ...).
I 17 use a lot of IllegalArgumentException when a method detects that 16 its parameters are incorrect. This is the 15 responsibility of any public method, to 14 stop processing (to avoid indirect errors 13 that are more difficult to understand). Also, a 12 few if
s in the beginning of a method serve 11 a documentation purpose (documentation that 10 never diverge from the code because it is 9 the code :-) ).
public void myMethod(String message, Long id) {
if (message == null) {
throw new IllegalArgumentException("myMethod's message can't be null");
// The message doesn't log the argument because we know its value, it is null.
}
if (id == null) {
throw new IllegalArgumentException("myMethod's id can't be null");
// This case is separated from the previous one for two reasons :
// 1. to output a precise message
// 2. to document clearly in the code the requirements
}
if (message.length()<12) {
throw new IllegalArgumentException("myMethod's message is too small, was '" + message + "'");
// here, we need to output the message itself,
// because it is a useful debug information.
}
}
I also use specific Runtime Exceptions to signal higher 8 level exceptional conditions.
For example, if 7 a module of my application couldn't start, I 6 might have a ModuleNotOperationalException thrown (ideally by a generic 5 code like an interceptor, otherwise by a 4 specific code) when another module calls 3 it. After that architectural decision, each 2 module has to deal with this exception on 1 operations that call other modules...
I've always considered that runtime exceptions 24 should represent programming errors (e.g. null
reference 23 passed in when not expected, array index 22 out of bounds, etc.) while checked exceptions 21 should represent exceptional conditions 20 in the environment that cannot be "coded 19 away" (e.g. IOException
, SQLException
).
One violation of this 18 is that sometimes you'll need to wrap what 17 ought to be a checked exception in a RuntimeException, in 16 order to satisfy the definition of an interface. As 15 a brief example, you might have some snazzy 14 implementation of java.util.List
that manages a distributed 13 list between multiple machines. Clearly 12 this would throw checked exceptions (probably 11 some subclass of IOException
) if defined on its own, but 10 the benefits of making this class implement 9 List
is that clients can use it almost transparently, anywhere 8 they use another list.
This can lead to what 7 Joel terms a leaky abstraction, though, so it's important 6 that your documentation is clear what exceptions 5 can be thrown and what they mean! In this 4 case I find a custom subclass of RuntimeException
to generally 3 be clearer at communicating the root cause 2 rather than trying to shoehorn it into an 1 existing runtime exception class.
I use IllegalArgumentException
relatively often. Most of the time, I 4 will try to return the default value as 3 soon as it is logical but some of the time 2 it was not, and so I use this one.
Another 1 one I use is ArrayIndexOutOfBoundsException
.
UnknownException, very usefull :P
i also 1 like org.apache.commons.lang.NotImplementedException
According to Joshua Bloch in Effective Java,
The most commonly reused 1 exceptions:
- IllegalArgumentException
- IllegalStateException
- NullPointerException
- IndexOutOfBoundsException
- ConcurrentModificationException
- UnsupportedOperationException
Mostly I don't throw runtime exception. Rather 5 than after checking specific conditions 4 throw user defined exceptions. But the few 3 I have used are - IllegalAccessException 2 , ArithmeticException, NumberFormatException 1 and SecurityException.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.