Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the conventional Java exception for "Too Many Results"

Tags:

java

exception

I am writing a generic API that takes parameters and returns results. I expect that if the data is consistent, only one result will be returned for an ID. If I get 1 result, I return it. If I get 0 results, I can throw "MissingResourceException" or "NoSuchElementException" etc. However, if I get multiple results, what should I throw? I have looked at the obvious places (Oracle/Java API documentation, Googling it, and of course, StackOverflow), but didn't find one.

Please note that I may not control the data, so the "the data should have been good/police your data" advice, while valid, will not help me.

Any help is appreciated.

like image 544
Prashant Avatar asked May 23 '16 21:05

Prashant


People also ask

How many exceptions can a method Throw in Java?

This signals that the method can eventually throw one of those two exceptions (and also any of the unchecked exceptions). You cannnot (in Java or in any language AFAIK) throw simultaneously two exceptions, that would not make much sense.

How many exceptions can be thrown by a method?

As seen in the syntax above, all exceptions that can be thrown by a method should be declared in the method signature using the throws keyword. A method can throw multiple exceptions, which should be separated by a comma in the declaration.

How do I fix exception error in Java?

The try-catch is the simplest method of handling exceptions. Put the code you want to run in the try block, and any Java exceptions that the code throws are caught by one or more catch blocks. This method will catch any type of Java exceptions that get thrown. This is the simplest mechanism for handling exceptions.

What are the types of exceptions in Java API?

There are mainly two types of exceptions in Java as follows: Checked exception. Unchecked exception.


1 Answers

Joshua Bloch's Effective Java says in Item 60 (Favor to use standard exceptions):

Also, feel free to subclass an existing exception if you want to add a bit more failure-capture information (Item 63).

So, I vote for one of IllegalStateException or YourOwnException (with additional failure-related info).

like image 84
Andrei_N Avatar answered Oct 13 '22 01:10

Andrei_N