Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 is that they are very black and white. Consider a use case where a method is parsing a file provided by the caller. The file exists, is readable, and is in the correct format. However, some content in the file is non-compliant with the business rules. What would be an appropriate exception to throw in this case - IllegalStateException or IllegalArgumentException?

Looking at various libraries that provide assertions, like Guava Preconditions or Spring Assert, it appears that there is no consensus. There are some good discussions here and here, but none provide a conclusive answer to the common use case I stated above.

like image 629
Abhijit Sarkar Avatar asked Feb 11 '18 20:02

Abhijit Sarkar


People also ask

When should you throw IllegalStateException?

The IllegalStateException is thrown when the Java environment or application is not in an appropriate state for the requested operation. This can occur when dealing with threads or the Collections framework of the java. util package under specific conditions.

When should you throw an IllegalArgumentException?

2.1. When we read the Javadoc for IllegalArgumentException, it says it's for use when an illegal or inappropriate value is passed to a method. We can consider a null object to be illegal or inappropriate if our method isn't expecting it, and this would be an appropriate exception for us to throw.

What is the difference between IllegalArgumentException and IllegalStateException?

The IllegalArgumentException is thrown in cases where the type is accepted but not the value, like expecting positive numbers and you give negative numbers. The IllegalStateException is thrown when a method is called when it shouldn't, like calling a method from a dead thread.

What is the use of IllegalArgumentException?

IllegalArgumentException is a Java exception indicating that a method has received an argument that is invalid or inappropriate for this method's purposes.


1 Answers

IllegalStateException is for coding errors, not input errors. It's for when the invariants of a class have been violated, or a method is called when an object is in the wrong state. Examples are using a closed resource, or closing a resource twice.

IllegalArgumentException is when an argument has an invalid value per the method API. Passing -1 when only positive numbers are allowed.

In this case neither exception is appropriate. I would create a sub-class of IOException since there's an error in an input file.

like image 102
John Kugelman Avatar answered Sep 24 '22 08:09

John Kugelman