是否可以在c#中进行Vectored Strstructed异常处理? [英] Is it possible to do Vectored Strctured exception handling in c#?

查看:133
本文介绍了是否可以在c#中进行Vectored Strstructed异常处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,当第一次机会异常发生时,将通知调试器(如果有),然后如果仍未处理,则系统在堆栈中搜索最近的基于帧的异常处理程序。

As far as i know, when a first chance exception occurs, the debugger is notified(if any) and then if still unhandled, the system searches for the nearest frame based exception handler in the stack if any.

我正在阅读此链接

问题1)我想知道是否有任何方法可以在托管代码中做到这一点?

Question1) I was wondering if there is any way we can do that in managed code?

问题2),我认为任何 try {} catch {} 都是框架基于处理程序,但当我们在某些事件(例如

Question2) I think that any try{}catch{} is a frame based handler but what happens when we register a handle at certain events like

AppDomain.CurrentDomain.UnhandledException += (x, y) =>
{
    Console.WriteLine("Unhandled exception");
};

这些是什么?

推荐答案

仅在调试器附加了异常时通知调试器。例如,如果您在Visal Studio中进行调试,则将首先调用它,如果它传递了异常,则将调用异常处理程序。若要使调试器停止所有异常,请从Visual Studio中选择调试菜单,然后选择异常,然后选择要调试器停止的异常。如果这些处理程序都不存在(或没有处理程序),则将第二次调用调试器以停止执行并显示一条通用消息,指出发生了异常。另一方面,如果未附加调试器,则不会通知调试器-所有托管生产代码中99%以上都是这种情况。

The debugger is only notified of an exception if it is attached. For example, if you are debugging in Visal Studio, then it would be called first, and if it passed on the exception, the exception handlers would be called. To have the debugger stop on all exceptions, from Visual Studio select the Debug menu and select Exceptions and then choose the exceptions you want the debugger to halt at. If none of those handlers (or there are no handlers) then the debugger would be called a 2nd time to stop execution and display a general message that an exception had occurred. If on the other hand, the debugger is not attached, then the debugger won't be notified - which is the case for 99%+ of all managed production code.

向量异常处理是旨在用于非托管代码(即C ++)的低级api。您从托管代码(C#)中的第一选择应该是对try / catch / finally块使用结构化异常处理。大多数(99%以上)使用C#之类的托管代码开发人员(包括我自己)都发现结构化异常处理已足够。

Vectored exception handling is a low level api intended to be used from unmanaged code - i.e. C++. Your very first choice from managed code (C#) should be to use structured exception handling with try/catch/finally blocks. Most (99%+) managed code developers using a language like C#, including myself, have found structured exception handling to be quite adequate.

如果您仍然认为需要使用此api,您将必须使用P / Invoke调用AddVectoredExceptionHandler,这是可以从托管代码中调用非托管代码的一种方式。有几个警告。请参见此链接,以获取概述 Mike Stall的博客建议在托管代码中避免向量化异常。 Mike Stall在此线程中说,非托管矢量异常处理程序永远不应调用托管代码。他还建议,托管异常是矢量异常处理中未记录的功能,MS可能会在将来的CLR版本中消除该支持,因此请自担风险。

If you still think you need to use this api, you will have to call AddVectoredExceptionHandler by using P/Invoke, which is one way unmanaged code can be called from managed code. There are a couple of caveats. See this link for a general overview. Mike Stall's blog suggests avoiding vectored exceptions in managed code. In this thread, Mike Stall says that unmanaged vector exception handlers should never call back into managed code. He also suggests that managed exceptions are an undocumented feature in vectored exception handling and that MS could eliminate that support in a future release of the CLR, so use at your own peril.

仅在以下情况下调用 AppDomain.UnhandledException事件找到其他处理程序。它可以用于需要记录有关未处理异常的某些信息的情况,但是几乎没有时间将适当的结构化异常处理放入大量现有代码中,或者您可能无法访问该代码这引发了异常。否则,应改用try / catch / finally块。

The AppDomain.UnhandledException event is only called when no other handler is found. It could be used in situations where it is desired to log some information about an unhandled exception, but there is little time to put in proper structured exception handling to a large base of existing code, or perhaps you don't have access to the code that is throwing the exception. Otherwise, try/catch/finally blocks should be used instead.

您还可以尝试查看 AppDomain.FirstChanceException事件,该事件在调用第一个异常处理程序之前发生。如果您要做的只是记录一些您无法访问的其他人代码中引发的某些异常的信息,则此方法可能会有用。它只是一个通知,而不是异常处理程序。我以前从未使用过它,所以不知道它会在多大程度上影响性能。我在这里担心的是,Microsoft开发人员有时会在CLR中针对真正错误以外的其他各种情况引发异常。若要查看实际效果,请尝试将调试器设置为在每个异常(包括CLR运行时异常)时都中断。这可以在Visual Studio中的调试菜单中选择例外来完成。因此,我不知道它将对性能产生多大影响,因为所有CLR异常也可能引发此事件。公平地讲,我上次这样做是在VS 2008中进行的。对于CLR的更高版本,这可能不是正确的。我也没有办法限制此事件将触发的异常。因此,您需要过滤掉您不感兴趣的异常,并尝试控制其对性能的影响。

You might also try taking a look at the AppDomain.FirstChanceException event, which occurs before the first exception handler is called. This one might be useful if all you want to do is log some information for certain exceptions thrown in someone else's code that you don't have access to that are handled. It is only a notification though, and not an exception handler. I have never used it before, so don't know how much it might affect performance. My concern here is that Microsoft developers sometimes throw exceptions in the CLR for various things other than true errors. To see this in action, try setting the debugger to break on every exception, including the CLR Runtime exceptions. This can be done in Visual Studio from the Debug menu and choosing Exceptions. So, I don't know how much of an impact it would have on performance since all the CLR exceptions will probably raise this event as well. To be fair, the last time I did this was with VS 2008. This might not be true or as true with later versions of the CLR. I don't see a way to limit the exceptions this event will fire either. So, you would need to filter out the exceptions you are not interested in and experiment to guage its impact on performance.

这篇关于是否可以在c#中进行Vectored Strstructed异常处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆