简体   繁体   English

什么是“throw new NotImplementedException();” 到底是做什么的?

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

I have a class 'b' that inherits from class 'a'.我有一个从类 'a' 继承的类 'b'。 In class 'a' there is some code that performs an action if an event is not null.在类“a”中有一些代码在事件不为空时执行操作。 I need that code to fire in class 'b' during specific times in the application.我需要在应用程序的特定时间在类“b”中触发该代码。 So in 'b' I subscribed to a new Handler(event).所以在 'b' 我订阅了一个新的 Handler(event)。

If I leave the autogenerated event 'as is' in class 'b' with the throw new NotImplementedException();如果我将自动生成的事件“按原样”留在类“b”中,并使用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.一旦我删除了 thow 异常,应用程序就不再按预期工作。

So, what is the throw new NotImplementedException doing 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.当代码“有效”时,if 语句返回 true。 When it isn't working as expected, it is returning 'false'.当它没有按预期工作时,它会返回“false”。 In class 'b', the only time the application 'works' (or the if statement returns true), is when I have the throw new NotImplementedException();在类“b”中,应用程序“有效”(或 if 语句返回 true)的唯一时间是当我有throw new NotImplementedException(); line in class 'b's event method that is autogenerated when I attached the new event.当我附加新事件时自动生成类 'b 的事件方法中的行。

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.这就是NotImplementedException的用途。

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).它不是与正常异常相反的“特殊”异常(除了像其他Exception一样从Exception派生)。 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.如果a有您需要在b使用的代码,请考虑使包含该代码的方法protected并且如果您需要override该行为,则可以选择使用virtual方法。

NotImplementedException is simply an exception that is used when the code for what you're trying to do isn't written yet. NotImplementedException只是在您尝试执行的代码尚未编写时使用的异常。 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.与空代码块相反,使用NotImplementedException是一种很好的做法,因为它会非常清楚地抛出一个错误,提醒您您的代码部分尚未完成。 If it was blank, then the method might run and nothing would happen, and that's a pain to debug sometimes.如果它是空白的,那么该方法可能会运行而不会发生任何事情,有时调试起来很痛苦。

NotImplementedException , I believe, has a little special meaning: "this code is still in development and needs to be changed".我相信NotImplementedException有一点特殊的含义:“这段代码仍在开发中,需要更改”。 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有关此异常类型的信息可以在文档中找到,它解释了非常详细的含义: 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.一些开发工具,例如 Resharper,会在内部生成带有NotImplementedException 的新成员,从​​而保护您免于执行未准备好的代码。 Same time they highlight this exceptions same way as "//todo:" comment同时他们以与“//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.我们使用默认的NotImplementedException生成两者,并使用第一个 ( Credit ) 来实现它。 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 ".他们正在尝试调用Transfer ,这向他们显示了一条一般消息“我们很抱歉,我们遇到了一些错误”,与此同时,作为开发团队,我们收到了有关NotImplementedException发生的通知,堆栈跟踪将我们指向方法“ 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.根据应用程序的目的,它可以是更大或更小的问题:可能是计算器上的按钮不起作用,或者可能是科学计算器,我们在计算针对某些侵略性病毒的疫苗代码时错过了一些重要的数学运算。

The NotImplementedException is a way of declaring that a particular method of an interface or base class is simply not implemented in your type. NotImplementedException是一种声明interface或基class的特定方法没有在您的类型中实现的方法。 This is the exception form of the E_NOTIMPL error code.这是E_NOTIMPL错误代码的异常形式。

In general an implementation shouldn't be throwing a NotImplementedException unless it's a specifically supported scenario for that particular interface .一般来说,一个实现不应该抛出NotImplementedException除非它是该特定interface的一个特别支持的场景。 In the vast majority of scenarios this is not the case and types should fully implement interfaces .在绝大多数情况下,情况并非如此,类型应该完全实现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.除非你给我们更多的信息,否则很难说为什么程序在遇到异常时保持功能并在没有异常的情况下中断。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM