c# – 如何获取有关异常的更多信息

前端之家收集整理的这篇文章主要介绍了c# – 如何获取有关异常的更多信息前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在 Windows 7,64位的visual studio 2008上创建了一个解决方案.

有用.

当我将它移动到另一台机器上时,也赢得了7,64位,它几乎没有信息就崩溃了.

最初的问题是:

call was rejected by callee

然后我实现了这个解决方

how to properly GetTypeFromProgID for visual studio 2008

但是,现在我的问题是,当我在另一台机器上运行可执行文件时,程序立即崩溃并显示以下信息:

Description:
  Stopped working

Problem signature:
  Problem Event Name:   APPCRASH
  Application Name: EmailSalesVolumeSolution.exe
  Application Version:  1.0.0.0
  Application Timestamp:    508064dd
  Fault Module Name:    KERNELBASE.dll
  Fault Module Version: 6.1.7601.17932
  Fault Module Timestamp:   503285c2
  Exception Code:   e0434f4d
  Exception Offset: 000000000000caed
  OS Version:   6.1.7601.2.1.0.256.48
  Locale ID:    1033

Read our privacy statement online:
  http://go.microsoft.com/fwlink/?linkid=104288&clcid=0x0409

If the online privacy statement is not available,please read our privacy statement offline:
  C:\Windows\system32\en-US\erofflps.txt

我将代码包装在try / catch中,但仍未收到正确的错误消息:

static void Main()
{
    try
    {
        EnvDTE80.DTE2 dte;
        object obj = null;
        System.Type t = null;

        // Get the ProgID for DTE 8.0.
        t = System.Type.GetTypeFromProgID("VisualStudio.DTE.9.0",true);
        // Create a new instance of the IDE.
        obj = System.Activator.CreateInstance(t,true);
        // Cast the instance to DTE2 and assign to variable dte.
        dte = (EnvDTE80.DTE2)obj;

        // Register the IOleMessageFilter to handle any threading
        // errors.
        MessageFilter.Register();
        // Display the Visual Studio IDE.
        dte.MainWindow.Activate();


        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        Application.Run(new Form1());

        // For example,get a reference to the solution2 object
        // and do what you like with it.

        // All done,so shut down the IDE...
        dte.Quit();
        // and turn off the IOleMessageFilter.
        MessageFilter.Revoke();

    }
    catch (Exception e)
    {
        MessageBox.Show(e.ToString());
    }
}

如何确定异常发生的确切位置以及异常是什么?

我有一些非托管代码

using System;
using System.Collections.Generic;
using System.Text;
using EnvDTE;
using EnvDTE80;
using EnvDTE90;
using System.Runtime.InteropServices;

namespace EmailSalesVolumeSolution
{
    public class MessageFilter : IOleMessageFilter
    {
        //
        // Class containing the IOleMessageFilter
        // thread error-handling functions.

        // Start the filter.
        public static void Register()
        {
            IOleMessageFilter newFilter = new MessageFilter();
            IOleMessageFilter oldFilter = null;
            CoRegisterMessageFilter(newFilter,out oldFilter);
        }

        // Done with the filter,close it.
        public static void Revoke()
        {
            IOleMessageFilter oldFilter = null;
            CoRegisterMessageFilter(null,out oldFilter);
        }

        //
        // IOleMessageFilter functions.
        // Handle incoming thread requests.
        int IOleMessageFilter.HandleInComingCall(int dwCallType,System.IntPtr hTaskCaller,int dwTickCount,System.IntPtr
          lpInterfaceInfo)
        {
            //Return the flag SERVERCALL_ISHANDLED.
            return 0;
        }

        // Thread call was rejected,so try again.
        int IOleMessageFilter.RetryRejectedCall(System.IntPtr
          hTaskCallee,int dwRejectType)
        {
            if (dwRejectType == 2)
            // flag = SERVERCALL_RETRYLATER.
            {
                // Retry the thread call immediately if return >=0 & 
                // <100.
                return 99;
            }
            // Too busy; cancel call.
            return -1;
        }

        int IOleMessageFilter.MessagePending(System.IntPtr hTaskCallee,int dwPendingType)
        {
            //Return the flag PENDINGMSG_WAITDEFPROCESS.
            return 2;
        }

        // Implement the IOleMessageFilter interface.
        [DllImport("Ole32.dll")]
        private static extern int
          CoRegisterMessageFilter(IOleMessageFilter newFilter,out 
          IOleMessageFilter oldFilter);
    }
    [ComImport(),Guid("00000016-0000-0000-C000-000000000046"),InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
    interface IOleMessageFilter
    {
        [PreserveSig]
        int HandleInComingCall(
            int dwCallType,IntPtr hTaskCaller,IntPtr lpInterfaceInfo);

        [PreserveSig]
        int RetryRejectedCall(
            IntPtr hTaskCallee,int dwRejectType);

        [PreserveSig]
        int MessagePending(
            IntPtr hTaskCallee,int dwPendingType);
    }
}

在给我带来麻烦的机器上安装VS 2008 express后,我现在得到这个:

解决方法

您还可以尝试订阅AppDomain.CurrentDomain.UnhandledException事件:
AppDomain.CurrentDomain.UnhandledException += 
      (sender,e) => MessageBox.Show(e.ExceptionObject.ToString());

在Main方法的顶部执行此操作.

原文链接:https://www.f2er.com/csharp/100470.html

猜你在找的C#相关文章