C# 什么是“抛出新的 NotImplementedException();” 到底是做什么的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10193284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does "throw new NotImplementedException();" do exactly?
提问by Mark
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).
我有一个从类 'a' 继承的类 'b'。在类“a”中有一些代码在事件不为空时执行操作。我需要在应用程序的特定时间在类“b”中触发该代码。所以在 'b' 我订阅了一个新的 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.
如果我将自动生成的事件“按原样”保留在“b”类中throw new NotImplementedException();,代码将按预期工作/运行。一旦我删除了 thow 异常,应用程序就不再按预期工作。
So, what is the throw new NotImplementedExceptiondoing besides throwing the exception?
那么,throw new NotImplementedException除了抛出异常还做什么呢?
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.
编辑:我意识到我对我的代码不是很具体。不幸的是,由于严格的政策,我不能。我在'a'类中有一个if语句。
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.
当代码“有效”时,if 语句返回 true。当它没有按预期工作时,它返回“假”。在类“b”中,应用程序“有效”(或 if 语句返回 true)的唯一时间是当我throw new NotImplementedException();在附加新事件时自动生成类“b”的事件方法中的行。
采纳答案by Adam Houldsworth
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 Exceptionlike 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.
它不是与正常异常相反的“特殊”异常(除了像其他异常Exception一样派生自)。您倾向于将它与代码生成一起视为实现它所抛出的成员的占位符。这样做比让代码生成尝试理解成员结构以输出编译代码要容易得多。
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 athat you need to use in b, consider making the method that houses the code protectedand optionally virtualif you need to overridethe behaviour.
如果有代码a,你需要在使用b,考虑使方法房屋代码protected和可选virtual,如果你需要override的行为。
回答by Adam Houldsworth
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 NotImplementedExceptionis used for.
...并且内部没有代码来执行此操作(该方法甚至不返回整数)。这是NotImplementedException用来做什么的。
回答by JaredPar
The NotImplementedExceptionis a way of declaring that a particular method of an interfaceor base classis simply not implemented in your type. This is the exception form of the E_NOTIMPLerror code.
这NotImplementedException是一种声明 aninterface或 base的特定方法class没有在您的类型中实现的方法。这是E_NOTIMPL错误代码的异常形式。
In general an implementation shouldn't be throwing a NotImplementedExceptionunless 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.
一般来说,一个实现不应该抛出 a ,NotImplementedException除非它是该特定interface. 在绝大多数情况下,情况并非如此,类型应该完全实现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.
就它在做什么而言。它只是抛出异常。除非你给我们更多的信息,否则很难说为什么程序在遇到异常时保持功能并在没有异常的情况下中断。
回答by Technoguyfication
NotImplementedExceptionis 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.
NotImplementedException只是一个例外,当您尝试执行的操作的代码尚未编写时使用。它通常在代码片段中用作占位符,直到您填写使用实际代码生成的任何内容的主体。
It's good practice to use a NotImplementedExceptionas 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.
使用 aNotImplementedException而不是空代码块是一种很好的做法,因为它会非常清楚地抛出一个错误,提醒您代码的部分尚未完成。如果它是空白的,那么该方法可能会运行而不会发生任何事情,有时调试起来很痛苦。
回答by Eugene Gorbovoy
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).
我相信NotImplementedException有一点特殊的含义:“这段代码仍在开发中,需要更改”。开发人员设置此异常是为了使某些代码可编译但不可执行(以避免数据损坏)。
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
有关此异常类型的信息可以在文档中找到,它解释了非常详细的含义: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 NotImplementedExceptioninside, thus protecting you from execution the code, which is not ready. Same time they highlight this exceptions same way as "//todo:" comment
一些开发工具,例如 Resharper,会在内部生成带有NotImplementedException 的新成员,从而保护您免于执行未准备好的代码。同时他们以与“//todo:”注释相同的方式突出显示此异常
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.
对于其他情况,例如,当您不需要实现接口或虚拟成员时,或者可能没有在 switch/case、if/else 等语句中实现某些路径时,您可能会使用 NotSupportedException、OutOfRangeException , ArgumentNullException, InvalidOperationException 等。
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:
最后,考虑这种情况以了解NotImplementedException的目的:我们正在编写银行应用程序,目前,实现汇款功能,我们有:
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 NotImplementedExceptionhappened 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.
这里我们调用了两个尚不存在的方法。我们使用默认的NotImplementedException生成两者,并使用第一个 ( Credit) 来实现它。可以说,实现需要一些时间,我们已经为它编写了测试,甚至还做了几次手动测试。我们完全忘记了第二种方法“借记”以及将我们的应用程序部署到测试版,甚至是生产。测试人员或用户开始使用我们的应用程序,很快他们就会使用汇款功能。他们正在尝试调用Transfer,这会向他们显示一条一般消息“我们很抱歉,我们遇到了一些错误”,与此同时,作为开发团队,我们收到了有关NotImplementedException 的通知堆栈跟踪将我们指向方法“ Debit”。现在我们正在实施它,并发布新版本,可以进行汇款。现在想象一下,如果那里没有例外,会发生什么:用户会花很多钱来尝试进行转账,尝试多次,每次都把钱扔进一个洞里。根据应用程序的目的,它可以是更大或更小的问题:可能是计算器上的按钮不起作用,或者可能是科学计算器,我们在计算针对某些侵略性病毒的疫苗代码时错过了一些重要的数学运算。

