处理 Visual Studio 2008 的调试器事件

发布于 2024-08-16 18:10:56 字数 1564 浏览 5 评论 0原文

我为 Visual Studio 创建了一个 AddIn,它应该处理用户调试应用程序并引发未处理的异常时的情况。我使用应用程序对象的“Events”属性注册了事件“OnExeceptionNotHandled”和“OnExceptionThrown”。在文档中可以看到这些事件在“OnEnterBreakMode”之前被触发。但是,当我调试一个引发“ArgumentException”的简单应用程序时,事件不会被触发。这是我的代码(缩短):

public class Connect : IDTExtensibility2
{
    public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
    {
        _applicationObject = (DTE2)application;
        _addInInstance = (AddIn)addInInst;

            _debuggerEvents = _applicationObject.Events.DebuggerEvents;
            _debuggerEvents.OnExceptionThrown += new _dispDebuggerEvents_OnExceptionThrownEventHandler(_debuggerEvents_OnExceptionThrown);
            _debuggerEvents.OnExceptionNotHandled += new _dispDebuggerEvents_OnExceptionNotHandledEventHandler(_debuggerEvents_OnExceptionNotHandled);

    }

        void _debuggerEvents_OnExceptionNotHandled(string ExceptionType, string Name, int Code, string Description, ref dbgExceptionAction ExceptionAction)
        {
            m_panOutput.OutputString("NotHandled\n");
        }

        void _debuggerEvents_OnExceptionThrown(string ExceptionType, string Name, int Code, string Description, ref dbgExceptionAction ExceptionAction)
        {
            m_panOutput.OutputString("Thrown\n");
        }

        void _debuggerEvents_OnEnterBreakMode(dbgEventReason Reason, ref dbgExecutionAction ExecutionAction)
        {
            m_panOutput.OutputString("EnterBreakMode\n");
        }

        DebuggerEvents _debuggerEvents;

}

I created an AddIn for Visual Studio, which should handle the case when the user debugs an application and an unhandled exception is thrown. I registered the events "OnExeceptionNotHandled" and "OnExceptionThrown" using the "Events" property of the application object. In the documentation one can read that these events get fired before the "OnEnterBreakMode". But when I debug a simple application which throws an "ArgumentException" the events do not get fired. Here's my code (shortened):

public class Connect : IDTExtensibility2
{
    public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
    {
        _applicationObject = (DTE2)application;
        _addInInstance = (AddIn)addInInst;

            _debuggerEvents = _applicationObject.Events.DebuggerEvents;
            _debuggerEvents.OnExceptionThrown += new _dispDebuggerEvents_OnExceptionThrownEventHandler(_debuggerEvents_OnExceptionThrown);
            _debuggerEvents.OnExceptionNotHandled += new _dispDebuggerEvents_OnExceptionNotHandledEventHandler(_debuggerEvents_OnExceptionNotHandled);

    }

        void _debuggerEvents_OnExceptionNotHandled(string ExceptionType, string Name, int Code, string Description, ref dbgExceptionAction ExceptionAction)
        {
            m_panOutput.OutputString("NotHandled\n");
        }

        void _debuggerEvents_OnExceptionThrown(string ExceptionType, string Name, int Code, string Description, ref dbgExceptionAction ExceptionAction)
        {
            m_panOutput.OutputString("Thrown\n");
        }

        void _debuggerEvents_OnEnterBreakMode(dbgEventReason Reason, ref dbgExecutionAction ExecutionAction)
        {
            m_panOutput.OutputString("EnterBreakMode\n");
        }

        DebuggerEvents _debuggerEvents;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦言归人 2024-08-23 18:10:56

根据我的经验,仅当禁用 Tools/Options/Debugging/General/Enable the exception Assistant 时,才会引发此事件。默认情况下启用此设置。

In my experience this event is only raised when Tools/Options/Debugging/General/Enable the exception assistant is disabled. This setting is enabled by default.

空宴 2024-08-23 18:10:56

不确定,但您可能需要重写基类的方法。

Not sure but you might need to override the methods of the base class.

心清如水 2024-08-23 18:10:56

我必须按照 Frank Koch 的建议关闭助手:工具/选项/调试/常规/启用异常助手被禁用。

我还按照这篇 MSDN 文章描述的方式连接了这些事件:

public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;

    Globals globals;
    globals = _applicationObject.Solution.Globals; 

    _debuggerEvents = globals.DTE.Events.DebuggerEvents;

    _debuggerEvents.OnEnterBreakMode += new _dispDebuggerEvents_OnEnterBreakModeEventHandler(BreakHandler);
    _debuggerEvents.OnExceptionThrown += new _dispDebuggerEvents_OnExceptionThrownEventHandler(_debuggerEvents_OnExceptionThrown);
    _debuggerEvents.OnExceptionNotHandled += new _dispDebuggerEvents_OnExceptionNotHandledEventHandler(_debuggerEvents_OnExceptionNotHandled);

...
}


void _debuggerEvents_OnExceptionNotHandled(string ExceptionType, string Name, int Code, string Description, ref dbgExceptionAction ExceptionAction)
{
    Debug.WriteLine("NotHandled\n");
}

void _debuggerEvents_OnExceptionThrown(string ExceptionType, string Name, int Code, string Description, ref dbgExceptionAction ExceptionAction)
{
    Debug.WriteLine("Thrown\n");
}

I had to turn the assistant off as Frank Koch suggests: Tools/Options/Debugging/General/Enable the exception assistant is disabled.

I also hooked up the events the way this MSDN article describes:

public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;

    Globals globals;
    globals = _applicationObject.Solution.Globals; 

    _debuggerEvents = globals.DTE.Events.DebuggerEvents;

    _debuggerEvents.OnEnterBreakMode += new _dispDebuggerEvents_OnEnterBreakModeEventHandler(BreakHandler);
    _debuggerEvents.OnExceptionThrown += new _dispDebuggerEvents_OnExceptionThrownEventHandler(_debuggerEvents_OnExceptionThrown);
    _debuggerEvents.OnExceptionNotHandled += new _dispDebuggerEvents_OnExceptionNotHandledEventHandler(_debuggerEvents_OnExceptionNotHandled);

...
}


void _debuggerEvents_OnExceptionNotHandled(string ExceptionType, string Name, int Code, string Description, ref dbgExceptionAction ExceptionAction)
{
    Debug.WriteLine("NotHandled\n");
}

void _debuggerEvents_OnExceptionThrown(string ExceptionType, string Name, int Code, string Description, ref dbgExceptionAction ExceptionAction)
{
    Debug.WriteLine("Thrown\n");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文