Reputation: 105217
Is there anything like .NET's NotImplementedException
in Java?
Upvotes: 621
Views: 200998
Reputation: 257001
In the spirit of Stack Overflow being a combination of Reddit and Wikipedia, here's some additional information, which is relevant to the question, and can also be an answer to the question.
When you ask NetBeans IDE to create a missing implementation, it uses an UnsupportedOperationException:
void setPropertiesWithReader(IDataReader rdr)
{
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
If it's good enough for NetBeans, it's good enough for us.
Upvotes: 4
Reputation: 2032
You could do it yourself (that's what I did) - in order to not be bothered with exception handling, you simply extend RuntimeException
. Your class could look something like this:
public class NotImplementedException extends RuntimeException {
private static final long serialVersionUID = 1L;
public NotImplementedException(){}
}
You could extend it to take a message - but if you use the method as I do (that is, as a reminder that there is still something to be implemented), then usually there is no need for additional messages.
I dare say, that I only use this method, while I am in the process of developing a system, makes it easier for me to not lose track of which methods are still not implemented properly :)
Upvotes: 60
Reputation: 5115
As mentioned, the JDK does not have a close match. However, my team occasionally has a use for such an exception as well. We could have gone with UnsupportedOperationException
as suggested by other answers, but we prefer a custom exception class in our base library that has deprecated constructors:
public class NotYetImplementedException extends RuntimeException
{
/**
* @deprecated Deprecated to remind you to implement the corresponding code
* before releasing the software.
*/
@Deprecated
public NotYetImplementedException()
{
}
/**
* @deprecated Deprecated to remind you to implement the corresponding code
* before releasing the software.
*/
@Deprecated
public NotYetImplementedException(String message)
{
super(message);
}
}
This approach has the following benefits:
NotYetImplementedException
, they know that an implementation was planned and was either forgotten or is still in progress, whereas UnsupportedOperationException
says (in line with collection contracts) that something will never be implemented. That's why we have the word "yet" in the class name. Also, an IDE can easily list the call sites.import
line (JDK 9 fixed this, though).Upvotes: 23
Reputation: 10493
Commons Lang has it. Or you could throw an UnsupportedOperationException
.
Upvotes: 586
Reputation: 26049
I think the java.lang.UnsupportedOperationException
is what you are looking for.
Upvotes: 336
Reputation: 1637
No there isn't and it's probably not there, because there are very few valid uses for it. I would think twice before using it. Also, it is indeed easy to create yourself.
Please refer to this discussion about why it's even in .NET.
I guess UnsupportedOperationException
comes close, although it doesn't say the operation is just not implemented, but unsupported even. That could imply no valid implementation is possible. Why would the operation be unsupported? Should it even be there?
Interface segregation or Liskov substitution issues maybe?
If it's work in progress I'd go for ToBeImplementedException
, but I've never caught myself defining a concrete method and then leave it for so long it makes it into production and there would be a need for such an exception.
Upvotes: 9