Originale-mail to me for new edition

 

Exceptions and runtime errors in libraries

 

When an exception is raised but not handled in a dynamically loadable library, it propagates out of the library to the caller. If the calling application or library is itself written in Object Pascal, the exception can be handled through a normal try...except statement.

Note:          Under Linux this is only possible if the library and application have both been built with the same set of (base) runtime packages (which contains the EH code) or if both link to ShareExcept.

If the calling application or library is written in another language, the exception can be handled as an operating-system exception with the exception code $0EEDFACE. The first entry in the ExceptionInformation array of the operating-system exception record contains the exception address, and the second entry contains a reference to the Object Pascal exception object.

Generally, you should not let exceptions escape from your library. On Windows, Delphi exceptions map to the OS exception model; Linux does not have an exception model.

If a library does not use the SysUtils unit, exception support is disabled. In this case, when a runtime error occurs in the library, the calling application terminates. Because the library has no way of knowing whether it was called from an Object Pascal program, it cannot invoke the application’s exit procedures; the application is simply aborted and removed from memory.

 

Topic groups

 

See also

Exceptions: Overview

Writing dynamically loadable libraries

 

 

译文

 

库中的异常和运行时错误

 

在动态可加载库中,当一个异常被引发但未被处理时,它将向库外传播,即传播到调用者。如果调用该库的应用程序或库,其自身也是由Object Pascal语言编写,那么异常可以通过一般的try...except语句被处理。

注意       Linux操作系统中,仅当包和应用程序都是用同一套基本的运行时包建立或都连接到ShareExcept时,上面的处理是可能的。

如果调用库的应用程序或库由其他语言编写,那么异常可以作为操作系统异常被处理,此时的异常代码是$0EEDFACE。在操作系统的异常记录数组ExceptionInformation中,第一个入口(即字段)包含了异常地址,第二个入口包含了一个对Object Pascal异常对象的引用。

通常,应当避免异常离开库。在Windows中,Delphi异常映射到操作系统(OS)异常模块;而Linux则没有类似的异常模块。

如果一个库没有使用SysUtils单元,那么它不能支持异常。这时,当运行时错误在库中发生时,调用该库的应用程序将终止。因为库无法获悉调用它的模块是否是Object Pascal程序,所以它不能调用应用程序的退出过程;应用程序只是简单地终止并从内存中删除。

 

主题组

 

相关主题

异常:概述

编写动态可加载库