当我调试程序并在立即窗口中尝试执行某些操作时,有时会在立即窗口中显示错误消息,内容为:“由于内存不足异常,函数评估已被禁用。”当将鼠标悬停在对象上查看其属性时,也会出现此错误消息。经过尝试找到问题的原因后,我将问题缩小到以下代码示例:
似乎调试器在OutOfMemoryException发生时会发生问题,即使已经捕获该异常...
我可以想象没有人曾经考虑过能够调试一个遇到OutOfMemoryException的程序。但不幸的是,当文件不是图像时,Image.FromFile会抛出该错误...
问题如下:
1.以上代码示例是否给其他人带来了问题?
2.有人能解释一下吗?为什么会发生这种情况?
3.最后,我该怎么防止这种情况发生?
using System;
using System.Text.RegularExpressions;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
try
{
//outofmemoryexception can be thrown by Image.FromFile("path/that/does/not/exist.png")
//if the path points to a file that is not an image
throw new OutOfMemoryException();
}
catch (OutOfMemoryException ex)
{
//caught the exception
//so no problem, right?
}
//Random object to use in immediate window
Random rand = new Random();
//Also, try hovering over this regex and take a look at its properties.
var test = new Regex("");
//put a breakpoint here (at the next closing curly brace) and try calling rand.Next() in the immediate window
}
}
}
似乎调试器在OutOfMemoryException发生时会发生问题,即使已经捕获该异常...
我可以想象没有人曾经考虑过能够调试一个遇到OutOfMemoryException的程序。但不幸的是,当文件不是图像时,Image.FromFile会抛出该错误...
问题如下:
1.以上代码示例是否给其他人带来了问题?
2.有人能解释一下吗?为什么会发生这种情况?
3.最后,我该怎么防止这种情况发生?