4

I have a class 'b' that inherits from class 'a'. In class 'a' there is some code that performs an action if an event is not null. I need that code to fire in class 'b' during specific times in the application. So in 'b' I subscribed to a new Handler(event).

If I leave the autogenerated event 'as is' in class 'b' with the throw new NotImplementedException(); line, the code works/runs as expected. As soon as I remove the thow exception, the application no longer works as expected.

So, what is the throw new NotImplementedException doing besides throwing the exception?

I realize I'm probably trying to solve my coding problem the wrong way at this point, and I am sure I will find a better way to do it (I'm still learning), but my question remains. Why does that line change the outcome of code?

EDIT: I reallize I wan't very specific with my code. Unfortunately, because of strict policies, I can't be. I have in class 'a' an if statement.

if (someEvent != null)

When the code 'works', the if statement is returning true. When it isn't working as expected, it is returning 'false'. In class 'b', the only time the application 'works' (or the if statement returns true), is when I have the throw new NotImplementedException(); line in class 'b's event method that is autogenerated when I attached the new event.

Mark
  • 150
  • 1
  • 3
  • 9

5 Answers5

6

Think about this: what if you want to add two integers with the following method...

private int Add(int x, int y)
{

}

...and have no code inside to do such (the method doesn't even return an integer). This is what NotImplementedException is used for.

4

NotImplementedException is simply an exception that is used when the code for what you're trying to do isn't written yet. It's often used in snippets as a placeholder until you fill in the body of whatever has been generated with actual code.

It's good practice to use a NotImplementedException as opposed to an empty block of code, because it will very clearly throw an error alerting you that section of your code isn't complete yet. If it was blank, then the method might run and nothing would happen, and that's a pain to debug sometimes.

Technoguyfication
  • 1,234
  • 9
  • 14
  • 1
    I believe this is only correct answer. NotImplementedException means, that in the future there should be some code written which will replace this exception. Or can be said: work hasn't finished here yet. Other answers in this topic assume other exceptions, like NotSupportedException, OutOfRangeException etc – Evgeny Gorbovoy May 14 '20 at 21:42
2

It is simply an exception, as for why it means your application "works" is entirely dependent on the code handling any exceptions.

It is not a "special" exception as opposed to a normal exception (other than being derived from Exception like the rest). You tend to see it with code generation as a placeholder for implementing the member it is throwing inside. It is a lot easier to do this than have code generation try to understand the member structure in order to output compiling code.

When you say "no longer works as expected", I am assuming it compiles. If removing this stops the code from compiling then the chances are good you have a compilation error about a return value.

Perhaps the code that triggers the event expects a certain response from handlers, or if there are no handlers or exceptions occur it defaults the response and carries on. In your case, there is a handler and no exception so it expects a better response?

Complete guesswork.

If there is code in a that you need to use in b, consider making the method that houses the code protected and optionally virtual if you need to override the behaviour.

Adam Houldsworth
  • 63,413
  • 11
  • 150
  • 187
  • Good point about the return value -- that possibility didn't occur to me -- but here we're talking about an event handler, which would be a `void` method. – phoog Apr 17 '12 at 14:41
  • 1
    @phoog True, but in lieu of any actual facts / code showing the issue, I can guess as wildly as a like ;-) – Adam Houldsworth Apr 17 '12 at 14:42
2

NotImplementedException, I believe, has a little special meaning: "this code is still in development and needs to be changed". Developers put this exception to make some code compileable, but not executable (to avoid data damage).

Information about this exception type can be found in documentation, it explains the meaning very detailed: https://learn.microsoft.com/en-us/dotnet/api/system.notimplementedexception?view=netcore-3.1

Some development tools, like Resharper, for example, generates new members with NotImplementedException inside, thus protecting you from execution the code, which is not ready. Same time they highlight this exceptions same way as "//todo:" comment

For other situations, for example, when you don't need to implement an interface or virtual member, or may be, you don't implement some paths in switch/case, if/else etc statements, you probably will use NotSupportedException, OutOfRangeException, ArgumentNullException, InvalidOperationException etc.

At the end, consider this situation to understand the purpose of NotImplementedException: We are writing banking application, and, at the moment, implementing money transferring feature, we have:

public void Transfer(sourceAccount, destAccount, decimal sum)
{
   sourceAccount.Credit(sum);
   destAccount.Debit(sum);
}

Here we are calling two methods which do not exist yet. We are generating both with default NotImplementedException, and going to first one (Credit) to implement it. Lets say, implementation took some time, we have written test for it, and even have done several manual tests. We completely forgot about second method "Debit" and deploying our application to beta, or even to production. Testers or users start using our application and soon they are coming to money transfer functionality. They are trying to call Transfer, which shows them a general message "We are so sorry, we got some errors", same time we, as a developer team, receive notification about NotImplementedException happened with the stack trace pointing us to the method "Debit". Now we are implementing it, and releasing new version, which can do money transfers. Now imagine, what would happen, if there was not an exception there: users would spend lot of money trying to do that transfers, trying several times, each time throwing money in to a hole. Depending on the purposes of the application it can be bigger or smaller problem: may be button on your calculator does not work, or may be that was scientific calculator and we just missed some important math while calculating vaccine code against some aggressive virus.

Evgeny Gorbovoy
  • 765
  • 3
  • 20
1

The NotImplementedException is a way of declaring that a particular method of an interface or base class is simply not implemented in your type. This is the exception form of the E_NOTIMPL error code.

In general an implementation shouldn't be throwing a NotImplementedException unless it's a specifically supported scenario for that particular interface. In the vast majority of scenarios this is not the case and types should fully implement interfaces.

In terms of what it's doing though. It's simply throwing an exception. It's hard to say why the program keeps function in the face of the exception and breaks without it unless you give us a bit more information.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • 1
    For an object that implements an interface, but does not provide behavior for a given interface member, isn't is more usual to throw a `NotSupportedException`? – phoog Apr 17 '12 at 14:46
  • @phoog `NotSupportedException` is usually reserved for cases where an interface has a property specifying whether or not a method on the interface is valid. For example if `ICollection.IsReadOnly` is true then `ICollection.Add` should throw `NotSupportedException`. A `NotImplementedException` is just for when a method is just not implemented. Write a blog post on the diff a while back http://blogs.msdn.com/b/jaredpar/archive/2008/12/12/notimplementedexception-vs-notsupportedexception.aspx – JaredPar Apr 17 '12 at 14:49
  • Exception NotImplementedException has good explaination in MSDN and it's nothing to do with interfaces or base classes – Evgeny Gorbovoy Apr 13 '21 at 10:20