Search code examples
c#exceptiontry-catchexecutionengineexception

why is my exception not catched?


i just had a problem while debugging which i don't get: I'm using a method from kernel32.dll to get the Free RAM, however it throws an System.EngineExecutionException, which I catch, but the Debugger stops anyway at the exception and refuses to continue. So why is the Exception not catched?

using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace StationController.Diagnose
{
public class Memorystatus
{
    public UInt32 Length;
    public UInt32 MemoryLoad;
    public UInt32 TotalPhys;
    public UInt32 AvailPhys;
    public UInt32 TotalPageFile;
    public UInt32 AvailPageFile;
    public UInt32 TotalVirtual;
    public UInt32 AvailVirtual;
}
class Test
{
    [DllImport("kernel32")]
    private static extern void GlobalMemoryStatus(ref Memorystatus buf);

    private static UInt32 GetFreeRAM()
    {
        try
        {
            Memorystatus MemStat=new Memorystatus();
            GlobalMemoryStatus(ref MemStat);
        }
        catch (System.ExecutionEngineException){ return 0; }
        catch (System.Exception) { return 0; }
        catch { return 0; } //I know kind of redundant
        return sMemStat.AvailPhys;
    }
}
}

Tools->Options->"Stop when Exception is outside AppDomain" is unchecked

I already fixed the reason for the exception, Memorystatus has to be a struct not a class, this question is about the try-catch behaviour


Solution

  • A System.EngineExecutionException is an internal .NET error which as far as I know is thrown by the CLR when something goes horribly wrong.

    Is it catchable? I don't think so - as I think an exception at this level prevents your code from continuing to execute

    http://social.msdn.microsoft.com/Forums/eu/clr/thread/4425321d-b291-4a2a-899c-8266f16e26d8

    It's probably due to messing with unmanaged memory - e.g. your kernel32.dll call

    http://msdn.microsoft.com/en-us/library/system.executionengineexception.aspx