Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

which exception to throw if list is empty in java?

Tags:

java

I have the following doubt concerning which exception to throw if list is empty

public class XYZ implements Runnable {     private List<File> contractFileList;      @Override     public void run() {          contractFileList = some method that will return the list;         //now i want to check if returned contractFile is empty or not , if yes then raise the exception         if (contractFileList.isEmpty()) {             // throw new ?????         }     } } 

I am runing this code inside a batch, I want to throw some exception that will stop the batch execution.

like image 868
Beginner Avatar asked Jun 26 '12 05:06

Beginner


People also ask

How do you throw an empty exception in Java?

This example shows how to handle the empty stack exception by using s. empty(), s. pop() methods of Stack class and System. currentTimeMillis()method of Date class.

Which will throw an exception when performed on an empty stack?

The EmptyStackException is thrown when attempting to access elements in an empty stack in Java. For example, if the Stack. pop() method is used to remove an object at the top of an empty stack, an EmptyStackException is thrown.

Can we use contains on empty list?

Your code won´t throw an exception if you call contains on an empty List. The only time you will receive an exception is in case the list was not initialised (NullPointerException).

What exceptions can you throw in Java?

There are two types of exceptions in Java: checked (compile time) exceptions and unchecked (runtime) exceptions.


1 Answers

That looks like IllegalStateException to me.

Signals that a method has been invoked at an illegal or inappropriate time.

Basically your object is not in a valid state for run to be called.

I wouldn't create your own exception for this unless you expect it to be deliberately caught elsewhere. It sounds like this would only occur due to a programming error rather than an unexpected situation... in which case an unchecked exception is appropriate, and IllegalStateException describes the general nature of the problem quite clearly.

You can put a detailed cause within the message of the exception (explaining that the "illegal state" was that the list was empty).

I suggest you try to avoid creating a separate exception type for every little thing that can go wrong - unless you're catching these exceptions separately, having different types doesn't help; it only adds to the clutter. An exception which is of the right broad type but has a useful message provides just as much benefit without as much cognitive overhead.

Note that you can't use a checked exception if you're implementing Runnable.run anyway, as that isn't declared to throw any checked exceptions. You'd have to wrap it in an unchecked exception (e.g. RuntimeException) at which point there's even less benefit.

like image 80
Jon Skeet Avatar answered Oct 12 '22 14:10

Jon Skeet