如何拦截WPF应用程序中的NotImplementedException?

时间:2022-05-06 23:52:51

How do I intercept a NotImplementedException in a WPF application?

如何拦截WPF应用程序中的NotImplementedException?

I'll occasionally throw a NotImplementedException while testing my in-progress WPF application:

在测试正在进行的WPF应用程序时,我偶尔会抛出一个NotImplementedException:

Private Sub ButtonDoSomething_Click(...) Handles ButtonDoSomething.Click
    Throw New NotImplementedException( _
        "ButtonDoSomething_Click() not implemented.")
End Sub

But, I'd rather these not crash the program.

但是,我宁愿这些也没有让程序崩溃。

I could replace all such exception throws with:

我可以用以下代码替换所有这些异常抛出:

MessageBox.Show("ButtonDoSomething_Click() not implemented.", _
    "Not Implemented", MessageBoxButton.OK, MessageBoxImage.Information)

But that seems inellegant somehow and wouldn't work if the NotImplementedException was buried away from the interface.

但是,如果将NotImplementedException从界面中隐藏起来,那么这似乎不合时宜并且不会起作用。

How can I capture all such exceptions and display a message box?

如何捕获所有此类异常并显示消息框?

3 个解决方案

#1


You can attach to the DispatcherUnhandledException event on the Application class, which will be raised anytime an unhandled exception occurs. In there, you can check the Exception property on the DispatcherUnhandledExceptionEventArgs instance passed to the event handler to see if it is of type NotImplementedException. If it is, then set the Handled property to true, and then return.

您可以附加到Application类上的DispatcherUnhandledException事件,该事件将在发生未处理的异常时生成。在那里,您可以检查传递给事件处理程序的DispatcherUnhandledExceptionEventArgs实例上的Exception属性,以查看它是否为NotImplementedException类型。如果是,则将Handled属性设置为true,然后返回。

It should be noted that if you called MsgBox instead of throwing the exception, you would have the problem of having to actually return something and set all out/ref parameters as well, which would have been additional overhead.

应该注意的是,如果你调用MsgBox而不是抛出异常,你就会遇到必须实际返回一些东西并设置所有out / ref参数的问题,这可能是额外的开销。

#2


Have a look at Application.DispatcherUnhandledException or AppDomain.UnhandledException. Both let you handle otherwise unhandled exceptions.

看看Application.DispatcherUnhandledException或AppDomain.UnhandledException。两者都允许您处理其他未处理的异常。

#3


Instead of throwing a NotImplementedException or using a MessageBox, you can use Debug.Fail. For example:

您可以使用Debug.Fail,而不是抛出NotImplementedException或使用MessageBox。例如:

Debug.Fail("This method is not implemented yet!");

It will pop up a message box with the text you provide and show you the stack trace.

它将弹出一个带有您提供的文本的消息框,并显示堆栈跟踪。

#1


You can attach to the DispatcherUnhandledException event on the Application class, which will be raised anytime an unhandled exception occurs. In there, you can check the Exception property on the DispatcherUnhandledExceptionEventArgs instance passed to the event handler to see if it is of type NotImplementedException. If it is, then set the Handled property to true, and then return.

您可以附加到Application类上的DispatcherUnhandledException事件,该事件将在发生未处理的异常时生成。在那里,您可以检查传递给事件处理程序的DispatcherUnhandledExceptionEventArgs实例上的Exception属性,以查看它是否为NotImplementedException类型。如果是,则将Handled属性设置为true,然后返回。

It should be noted that if you called MsgBox instead of throwing the exception, you would have the problem of having to actually return something and set all out/ref parameters as well, which would have been additional overhead.

应该注意的是,如果你调用MsgBox而不是抛出异常,你就会遇到必须实际返回一些东西并设置所有out / ref参数的问题,这可能是额外的开销。

#2


Have a look at Application.DispatcherUnhandledException or AppDomain.UnhandledException. Both let you handle otherwise unhandled exceptions.

看看Application.DispatcherUnhandledException或AppDomain.UnhandledException。两者都允许您处理其他未处理的异常。

#3


Instead of throwing a NotImplementedException or using a MessageBox, you can use Debug.Fail. For example:

您可以使用Debug.Fail,而不是抛出NotImplementedException或使用MessageBox。例如:

Debug.Fail("This method is not implemented yet!");

It will pop up a message box with the text you provide and show you the stack trace.

它将弹出一个带有您提供的文本的消息框,并显示堆栈跟踪。