情况:我有一个在硬盘上增长的列表,我想将其加载到内存中进行处理.在某些时候,我会得到EOutofMemory.我需要的是一种测试我的except块中的代码的方法:
implementation
uses sysutils;
function loadit(var F:File);
begin
Try
{load list code}
except
on EOutofMemory do
begin
{error handling code to be tested}
end;
end; // the try block
end;
Run Code Online (Sandbox Code Playgroud)
我需要的是调用异常块而不实际等待满足内存不足的情况.我看过加注,我没有找到任何可以按我需要的方式引发异常的东西.
raise EOutofMemory;
Run Code Online (Sandbox Code Playgroud)
不编译.
raise;
Run Code Online (Sandbox Code Playgroud)
编译,但提出了什么?
raise EOutofMemory.NewInstance;
Run Code Online (Sandbox Code Playgroud)
编译,但我想知道它后面的对象是做什么的?它会在我离开错误处理块,留在堆中还是编译器决定使用它时被销毁?
断言不允许指定任何内容; 无论如何,这不是它的目标.
由于在内存不足时没有足够的内存来创建异常,因此EOutOfMemory在启动时预先分配了内存.您使用它而不是创建一个新的.作为documentation州:
永远不要直接提高EOutOfMemory.而是调用全局 OutOfMemoryError过程.
Try
OutOfMemoryError;
except
on EOutofMemory do
begin
{error handling code to be tested}
end;
end; // the try block
Run Code Online (Sandbox Code Playgroud)