What does “throw new NotImplementedException();” do exactly?

前端 未结 5 564
Happy的楠姐
Happy的楠姐 2021-01-23 06:01

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\'

相关标签:
5条回答
  • 2021-01-23 06:19

    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.

    0 讨论(0)
  • 2021-01-23 06:22

    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://docs.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.

    0 讨论(0)
  • 2021-01-23 06:31

    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.

    0 讨论(0)
  • 2021-01-23 06:33

    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.

    0 讨论(0)
  • 2021-01-23 06:40

    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.

    0 讨论(0)
提交回复
热议问题