625

Is there anything like .NET's NotImplementedException in Java?

6 Answers 6

590

Commons Lang has it. Or you could throw an UnsupportedOperationException.

Sign up to request clarification or add additional context in comments.

5 Comments

It appears that NotImplementedException has been removed from Commons Lang 3.0.
I think since the UnsupportedOperationException is part of the collections framework, it should only be used if it is used in the conext of Collections. Otherwise a RuntimeException should be used. docs.oracle.com/javase/7/docs/technotes/guides/collections/…
@LeonardButz It comes from java.lang: docs.oracle.com/javase/1.5.0/docs/api/java/lang/…
@RaviWallau I saw this: docs.oracle.com/javase/7/docs/api/java/lang/… There stands that this class is a member of the Java Collection Framework.
It has been readded in Commons Lang 3.2: commons.apache.org/proper/commons-lang/javadocs/api-3.2
340

I think the java.lang.UnsupportedOperationException is what you are looking for.

8 Comments

I say it is something quite different. The NIE also tells it may not implemented yet, where the UOE says me it never will...
@Dykam, then wouldn't it be a NotImplementedYetException?
@Dykam: new UnsupportedOperationException("Not implemented yet") - happy?
I didn't mean it was worse, just had a different use case.
new UnsupportedOperationException("Not implemented yet") is an awesome idea! :) in lang3 for some reason I don't have NotImplementedException so this is a great solution
|
60

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 :)

3 Comments

I like this solution the best because it's easy to have a special error handler for it, it's easy to search for it by finding all references to the NotImplementedException constructor, and it's just a few lines of code. But it is a bit inconvenient to have to declare a new class with its own file.
I agree. This is better than the use of UnsupportedOperationException in my opinion. Now, if only Java would add this to the common library of exceptions!
Actually, UnsupportedOperationException also extends RuntimeException, and it supports an optional message.
23

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:

  1. When readers see 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.
  2. With the deprecation warning at each call site, your IDE and static code analysis tool can remind you where you still have to implement something. (This use of deprecation may feel wrong to some, but in fact deprecation is not limited to announcing removal.)
  3. The constructors are deprecated, not the class. This way, you only get a deprecation warning inside the method that needs implementing, not at the import line (JDK 9 fixed this, though).

Comments

9

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.

Comments

4

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.