简体   繁体   English

为什么我的例外没有被发现?

[英]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. 我在调试时遇到了一个我没有得到的问题:我正在使用kernel32.dll中的一种方法来获取Free RAM,但是它抛出了System.EngineExecutionException,我抓住了它,但是调试器无论如何都会在异常时停止并拒绝继续。 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 取消选中“工具”->“选项”->“在AppDomain外部出现异常时停止”

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 我已经解决了异常的原因,Memorystatus必须是一个结构而不是一个类,这个问题是关于try-catch行为的

A System.EngineExecutionException is an internal .NET error which as far as I know is thrown by the CLR when something goes horribly wrong. System.EngineExecutionException是一个内部.NET错误,据我所知,当发生严重错误时,CLR会抛出该错误。

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 http://social.msdn.microsoft.com/Forums/eu/clr/thread/4425321d-b291-4a2a-899c-8266f16e26d8

It's probably due to messing with unmanaged memory - eg your kernel32.dll call 这可能是由于混乱了非托管内存-例如您的kernel32.dll调用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM