607

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

Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162
devoured elysium
  • 101,373
  • 131
  • 340
  • 557

6 Answers6

582

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

Community
  • 1
  • 1
Ravi Wallau
  • 10,416
  • 2
  • 25
  • 34
  • 27
    It appears that NotImplementedException has been removed from Commons Lang 3.0. – Michael Younkin Aug 19 '11 at 15:17
  • 16
    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. http://docs.oracle.com/javase/7/docs/technotes/guides/collections/index.html – L.Butz May 16 '12 at 14:06
  • 13
    @LeonardButz It comes from java.lang: http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/UnsupportedOperationException.html – Ravi Wallau May 24 '12 at 19:25
  • 5
    @RaviWallau I saw this: http://docs.oracle.com/javase/7/docs/api/java/lang/UnsupportedOperationException.html There stands that this class is a member of the Java Collection Framework. – L.Butz Jul 11 '12 at 10:56
  • 5
    It has been readded in Commons Lang 3.2: http://commons.apache.org/proper/commons-lang/javadocs/api-3.2/ – qwertzguy Jan 13 '15 at 22:39
332

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

user11153
  • 8,536
  • 5
  • 47
  • 50
Chris Dail
  • 25,715
  • 9
  • 65
  • 74
  • 33
    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 Feb 24 '10 at 21:01
  • 5
    @Dykam, then wouldn't it be a NotImplementedYetException? – Yishai Feb 24 '10 at 21:06
  • 126
    @Dykam: `new UnsupportedOperationException("Not implemented yet")` - happy? – Michael Borgwardt Feb 24 '10 at 21:08
  • 3
    I didn't mean it was worse, just had a different use case. – Dykam Feb 25 '10 at 05:46
  • 6
    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 – ufk Jul 06 '11 at 09:35
  • 3
    @Dykam In .NET, `NotSupportedException` and `NotImplementedException` have very different meanings. In Java, however, the de facto standard is using `UnsupportedOperationException` in both cases, with the message indicating that the method is not yet implemented (where applicable). Even if you use a custom `NotImplementedException` for your own code, you can never assume in Java that `UnsupportedOperationException` means the method "won't" be implemented. – Sam Harwell Jan 18 '14 at 22:22
  • 1
    Funny that you comment, started using Java more often not too long ago, and this is in the Apache libs: [`NotImplementedException`](http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/NotImplementedException.html). Which inherits `UnsupportedOperationException`. – Dykam Jan 19 '14 at 00:17
  • 2
    FWIW this is how the Netbeans IDE implements auto-generated methods. Actually Netbeans also adds a comment: `throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.` – Ryan Jun 04 '15 at 00:39
59

You could do it yourself (thats what I did) - in order to not be bothered with exception handling, you simply extend the 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 :)

Daniel Lyons
  • 22,421
  • 2
  • 50
  • 77
Ready4Android
  • 2,022
  • 2
  • 25
  • 29
  • 3
    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. – Chiara Coetzee Mar 31 '12 at 13:19
  • 1
    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! – crush Sep 05 '13 at 12:50
  • Actually, `UnsupportedOperationException` also extends `RuntimeException`, and it supports an optional message. – bvdb Jul 01 '20 at 13:08
19

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).
Jens Bannmann
  • 4,845
  • 5
  • 49
  • 76
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.

Community
  • 1
  • 1
Dormouse
  • 1,617
  • 1
  • 23
  • 33
3

In the spirit of Stackoverflow 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 a 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.

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219