Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the standard exception to throw in Java for not supported/implemented operations?

Tags:

java

In particular, is there a standard Exception subclass used in these circumstances?

like image 511
Krishna Kumar Avatar asked May 06 '09 11:05

Krishna Kumar


People also ask

What exceptions can you throw in Java?

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

Which exceptions can not be handled in Java?

Unchecked exceptions are error conditions that cannot be anticipated and recovered from. They are usually programming errors and cannot be handled at runtime. Unchecked exceptions are subclasses of java.

Can you throw a general exception in Java?

You can throw a more general exception, or a more specific exception. For simpler methods, more general exceptions are enough. If the method is complex, then, throwing a more specific exception will be reliable.

What are runtime exceptions in Java give example?

Runtime - runtime exceptions are internal to your application but are not typically recoverable. For example, an object that is expected to have a value but is actually null. In this case, a NullPointerException exception would be thrown.


Video Answer


2 Answers

java.lang.UnsupportedOperationException

Thrown to indicate that the requested operation is not supported.

like image 195
dfa Avatar answered Oct 11 '22 12:10

dfa


Differentiate between the two cases you named:

  • To indicate that the requested operation is not supported and most likely never will, throw an UnsupportedOperationException.

  • To indicate the requested operation has not been implemented yet, choose between this:

    1. Use the NotImplementedException from apache commons-lang which was available in commons-lang2 and has been re-added to commons-lang3 in version 3.2.

    2. Implement your own NotImplementedException.

    3. Throw an UnsupportedOperationException with a message like "Not implemented, yet".

like image 34
steffen Avatar answered Oct 11 '22 10:10

steffen