How to catch "Unhandled win32 exception occured in AppName [procId]."

hbk picture hbk · Mar 16, 2014 · Viewed 9.9k times · Source

using c#, Visual studio 2013, Windows Store App

A little bit long explanation

Create some simple Windows Store App that works with JSON stored data. After increasing of data amount I start to get a message Unhandled win32 exception occured in AppName [procId]. - please see pic below:

enter image description here

I try to reduce amount of stored data in JSON file, but after some time off work during debugging I got this message again. So situation - if I have a lot of data - I can do few operations (few means 5) in app and got this exception, if I have minimum amount of data I can work with app a little bit more (mean 12-17 different operation). Operation means - read from file, save, load pages etc.

I googling a little bit and found few possible causes:

  • I must to setup DEP at PC, following next steps:

    1. Right click on “My Computer”. Then select “Properties”.
    2. Select “Advanced” Tab.
    3. Select “Settings” for “Performance”.
    4. Select “Data Execution Prevention” Tab.
    5. Select option “Turn on DEP for essential Windows programs and services only.” If this option is already selected, click on “OK”, then again click on “OK”.
    6. Restart your computer.

Try - not helps

  • Check and enabling just in time debugging in my VISUAL STUDIO

enter image description here

Try - not helps

  • Check what type of exception can be catches by VS - choose all:

enter image description here

Try - not helps

Found next:

An unhandled win32 exception occurred in . Just-In-Time debugging this exception failed with the following error: The logged in user did not have access to debug the crashing application. This message indicates that Just-In-Time debugging failed because you do not have proper access permissions.

So, mean you do not have proper access permissions.

Try to launch with Administrator rights my App:

enter image description here

Try - not helps

  • Also read a lot posts from here.

Found this and so this MSDN post useful. Try to add some code to my app:

    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();
    }

but nothing catched...

So, try - not helps

Questions:

  1. Why I got this message and what possible reasons that I not describe can be root cause for exception like "Unhandled win32 exception occured in AppName [procId]."?
  2. Am I right understand usage of UnhandledException? Maybe I'm wrong with it and so I can't catch required exception (I'm just studying for .NET)?

Answer

Behzad picture Behzad · Jul 18, 2015

A few months ago, I designed a Error Control System for this job actually. In this project I used this main codes to catch any win32 application exceptions:

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;

And listener methods:

/// <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 ...        
}

If throw an exception within your codes, you must be sure to catch that by this events, but when a exception occurred before executing application then this events don't raised to show or do any things.
For example your are not exactly added all references assemblies to your project then this caused to throw an exception in application startup times.

And some another exceptions maybe have innerException because raised from a thread or tasks. So you must be checked all exception.innerException.

I'm hope to presented a solution for your problem.