【问题标题】:How to catch "Unhandled win32 exception occured in AppName [procId]."如何捕获“AppName [procId] 中发生未处理的 win32 异常”。
【发布时间】:2014-04-22 00:26:41
【问题描述】:

使用 c#、Visual Studio 2013、Windows Store 应用程序

解释有点长

创建一些适用于 JSON 存储数据的简单 Windows 应用商店应用。数据量增加后,我开始收到一条消息Unhandled win32 exception occured in AppName [procId]. - 请参见下图:

我尝试减少 JSON 文件中存储的数据量,但在调试期间下班一段时间后,我再次收到此消息。所以情况 - 如果我有很多数据 - 我可以在应用程序中执行很少的操作(很少意味着 5)并得到这个异常,如果我有最少的数据量我可以使用应用程序多一点(平均 12-17 个不同手术)。操作方式——读取文件、保存、加载页面等

谷歌搜索了一下,发现了几个可能的原因:

  • 我必须在 PC 上设置 DEP,执行以下步骤:

    1. 右键单击“我的电脑”。然后选择“属性”。
    2. 选择“高级”选项卡。
    3. 为“性能”选择“设置”。
    4. 选择“数据执行保护”选项卡。
    5. 选择“仅对基本的 Windows 程序和服务启用 DEP”选项。如果已选择此选项,请单击“确定”,然后再次单击“确定”。
    6. 重新启动计算机。

试试 - 没用

  • 在我的 VISUAL STUDIO 中检查并启用即时调试

试试 - 没用

  • 检查 VS 可以捕获什么类型的异常 - 全选:

试试 - 没用

找到下一个:

在 .即时调试此异常失败并出现以下错误:登录的用户无权调试崩溃的应用程序。 此消息表明即时调试失败,因为您没有适当的访问权限。

所以,意思是您没有适当的访问权限

尝试以管理员权限启动我的应用程序:

试试 - 没用

  • 还看了很多here的帖子。

发现thisthis MSDN 的帖子很有用。尝试在我的应用中添加一些代码:

    public MainPage()
    {
        this.InitializeComponent();
        this.navigationHelper = new NavigationHelper(this);
        this.navigationHelper.LoadState += navigationHelper_LoadState;
        this.navigationHelper.SaveState += navigationHelper_SaveState;
        TimeBinding();
        Application.Current.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);
    }

    static void MyHandler(object sender, UnhandledExceptionEventArgs args)
    {
        string e = args.Message.ToString();
    }

但是什么都没抓到……

所以,试试 - 没用

问题:

  1. 为什么我收到此消息以及我未描述的哪些可能原因可能是导致 "Unhandled win32 exception occured in AppName [procId]." 等异常的根本原因?
  2. 我正确理解UnhandledException 的用法吗?也许我错了,所以我无法捕获所需的异常(我只是在为 .NET 学习)?

【问题讨论】:

    标签: c# .net exception-handling windows-store-apps visual-studio-2013


    【解决方案1】:

    几个月前,我实际上为这项工作设计了Error Control System。 在这个项目中,我使用这个主要代码来捕获任何 win32 应用程序异常:

    System.Windows.Forms.Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
    
    // Catch all handled exceptions in managed code, before the runtime searches the Call Stack 
    AppDomain.CurrentDomain.FirstChanceException += FirstChanceException;
    
    // Catch all unhandled exceptions in all threads.
    AppDomain.CurrentDomain.UnhandledException += UnhandledException;
    
    // Catch all unobserved task exceptions.
    TaskScheduler.UnobservedTaskException += UnobservedTaskException;
    
    // Catch all unhandled exceptions.
    System.Windows.Forms.Application.ThreadException += ThreadException;
    
    // Catch all WPF unhandled exceptions.
    Dispatcher.CurrentDispatcher.UnhandledException += DispatcherUnhandledException;
    

    和监听器方法:

    /// <summary>
    /// Used for handling WPF exceptions bound to the UI thread.
    /// Handles the <see cref="System.Windows.Threading.DispatcherUnhandledExceptionEventHandler"/> events.
    /// </summary>
    [HandleProcessCorruptedStateExceptions]
    private static DispatcherUnhandledExceptionEventHandler DispatcherUnhandledException
    {
        // catch error ...            
    }
    
    /// <summary>
    /// Used for handling WinForms exceptions bound to the UI thread.
    /// Handles the <see cref="System.Threading.ThreadExceptionEventHandler"/> events in <see cref="System.Windows.Forms.Application"/> namespace.
    /// </summary>
    [HandleProcessCorruptedStateExceptions]
    private static ThreadExceptionEventHandler ThreadException
    {
        // catch error ...        
    }
    
    /// <summary>
    /// Used for handling general exceptions bound to the main thread.
    /// Handles the <see cref="AppDomain.UnhandledException"/> events in <see cref="System"/> namespace.
    /// </summary>
    [HandleProcessCorruptedStateExceptions]
    private static UnhandledExceptionEventHandler UnhandledException
    {
        // catch error ...        
    }
    
    /// <summary>
    /// Used for handling System.Threading.Tasks bound to a background worker thread.
    /// Handles the <see cref="UnobservedTaskException"/> event in <see cref="System.Threading.Tasks"/> namespace.
    /// </summary>
    [HandleProcessCorruptedStateExceptions]
    private static EventHandler<UnobservedTaskExceptionEventArgs> UnobservedTaskException
    {
        // catch error ...        
    }
    
    /// <summary>
    /// This is new to .Net 4 and is extremely useful for ensuring that you ALWAYS log SOMETHING.
    /// Whenever any kind of exception is fired in your application, a FirstChangeExcetpion is raised,
    /// even if the exception was within a Try/Catch block and safely handled.
    /// This is GREAT for logging every wart and boil, but can often result in too much spam, 
    /// if your application has a lot of expected/handled exceptions.
    /// </summary>
    [HandleProcessCorruptedStateExceptions]
    private static EventHandler<FirstChanceExceptionEventArgs> FirstChanceException
    {
       // catch error ...        
    }
    

    如果在您的代码中引发异常,您必须确保通过此事件捕获该异常,但是当在执行应用程序之前发生异常时,不会引发此事件来显示或执行任何操作。
    例如,您没有将所有引用程序集完全添加到您的项目中,然后这会导致在应用程序启动时引发异常。

    还有一些其他异常可能有 innerException,因为它是从 threadtasks 引发的。所以你必须检查所有exception.innerException

    我希望为您的问题提出解决方案。

    【讨论】:

      猜你喜欢
      • 2019-11-01
      • 2020-05-25
      • 1970-01-01
      • 2012-09-05
      • 1970-01-01
      • 1970-01-01
      • 2011-03-12
      • 1970-01-01
      • 2015-09-10
      相关资源
      最近更新 更多