What are the most commonly used runtime exceptions in java?

As a Java programmer who wants to improve my programming skills, I often come across situations that I should throw an exception at runtime. I know this is a good practice if you use wisely.

Personally, NullPointerException and IllegalStateException are most often used in the software we created. What about you?

What runtime exceptions do you often use? In what situations do you use them?

+42
java exception runtime
01 Oct '09 at 9:19
source share
6 answers

I never throw a NullPointerException . For me, this looks natural in the code when something goes wrong, and this requires the developer to see what happens. Then (s) he corrects the cause, and this does not happen again.

I am using IllegalStateException to signal that the object is configured incorrectly or that the calls are in the wrong order. However, we all know that, ideally, an object should guarantee that it cannot be in a bad state and that you cannot call it in the wrong order (create a constructor and the resulting object ...).

I use a lot of IllegalArgumentException when a method detects that its parameters are incorrect. It is the responsibility of any public method to stop processing (to avoid indirect errors that are more difficult to understand). In addition, several if at the beginning of the method serve the purpose of documentation (documentation that never diverges from code because it is code :-)).

  public void myMethod(String message, Long id) { if (message == null) { throw new IllegalArgumentException("myMethod 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 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 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 exceptional conditions at a higher level.

For example, if the module of my application could not be started, I may get a ModuleNotOperationalException exception (ideally a common code, for example, an interceptor, otherwise a specific code) when another module calls it. After this architectural decision, each module must deal with this exception in operations that call other modules ...

+66
01 Oct '09 at 9:26
source share

I always thought that runtime exceptions should represent programming errors (for example, a null reference is passed when it is not expected, the array index is out of bounds, etc.), and checked exceptions should represent exceptional conditions in an environment that cannot be "(e.g. IOException , SQLException ).

One such violation is that sometimes you need to wrap what should be a checked exception in a RuntimeException in order to satisfy the interface definition. As a short example, you might have some realistic implementation of java.util.List that manages a distributed list between multiple machines. Obviously, this would exclude checked exceptions (perhaps some subclass of IOException ) if they are defined on their own, but the advantages of creating this List class are that clients can use it almost transparently wherever they use a different list.

This can cause Joel to mean a missing abstraction , so it is important that your documentation makes it clear which exceptions can be thrown and what they mean! In this case, I find that the custom subclass of RuntimeException tends to be clearer when passing the root cause, rather than trying to translate it into the existing runtime exception class.

+11
Oct 01 '09 at 9:44
source share

I use IllegalArgumentException relatively often. In most cases, I will try to return the default value as soon as this is logical, but at a time when it is not, and therefore I use this one.

Another one I'm using is an ArrayIndexOutOfBoundsException .

+3
01 Oct '09 at 9:28
source share

Unknown exception, very useful: P

I also like org.apache.commons.lang.NotImplementedException

+2
01 Oct '09 at 12:40
source share

Basically I do not throw an exception at runtime. Instead of checking for specific conditions, you can throw out user-defined exceptions. But the few that I used are IllegalAccessException, ArithmeticException, NumberFormatException and SecurityException.

+2
Jul 11 '13 at 5:25
source share

According to Joshua Bloch in Effective Java,

The most common exceptions:

  • IllegalArgumentException
  • IllegalStateException
  • Nullpointerinterception
  • IndexOutOfBoundsException
  • ConcurrentModificationException
  • UnsupportedOperationException
0
Dec 14 '17 at 4:32
source share



All Articles