Root > Advanced topics > Using EurekaLog in DLL > Introduction > What is exception

What is exception

Previous pageReturn to chapter overviewNext page   

Exception is represented by an object (class instance) in most modern high-level programming languages. This means that exceptions can be inherited from base classes, as well as be extended with arbitrary properties.

 

 

Hardware level

Since exception is a way to interrupt normal execution path of a code - it requires support from hardware level. Modern CPUs provides such support. However, user-mode applications do not have direct access to the hardware. Therefore, operating system provides method to use exceptions on particular hardware. This is called SEH ("Structured Exception Handling") in Windows.

 

 

OS level

Exception on operating system level is represented by its address, code, options ("flags") and up to 15 integers ("params").

 

_EXCEPTION_RECORD = record
  ExceptionCode: DWORD;
  ExceptionFlags: DWORD;
  ExceptionRecord: PExceptionRecord;
  ExceptionAddress: Pointer;
  NumberParameters: DWORD;
  ExceptionInformation: array[0..EXCEPTION_MAXIMUM_PARAMETERS - 1] of DWORD;
end;

 

Basically, it is a simple record. It contains only simple data types. Therefore, any programming language is able to understand it. E.g. exceptions on operating system level can safely travel between executable modules (DLLs/exe).

 

 

Language level

High-level programming languages use SEH and the above low-level representation as basis for their own exception handling. For example, exception in high-level programming language (i.e. exception object) is implemented as OS exception with special code (for example: $EEECFADE for Delphi) and a pointer to object is stored in exception params. Exceptions with other codes are wrapped in generic class (EExternalException for Delphi). Pseudo-code:

 

var

  E: Exception;          // Exception = class(TObject)
  ER: _EXCEPTION_RECORD; // Simple record - see above
 
ER.ExceptionCode := $EEECFADE;
ER.ExceptionInformation[1] := DWORD(E);

 

To properly handle such exceptions - programming language must know a "magic" code ($EEECFADE in the example above), as well as internal structure (memory layout) of Delphi objects. Naturally, a Microsoft's Visual Studio DLL does not know that.

 

Even various Delphi versions may be incompatible between each other. For example, a .Message property of exception object is an ANSI string in Delphi 2007 and earlier, and it is Unicode string in Delphi 2009 and later. Thus, Delphi 2007 will fail to process exception from Delphi 2009 and visa versa. In other words, if you are going to exchange language-specific exceptions between modules - both modules has to be compiled in the same version of the compiler.

 

 

Conclusion

Short conclusion:

1. There are 3 levels of exceptions support: hardware, OS, and programming language.
2. User-mode code has access to OS and language levels.
3. OS exceptions are compatible among all programming languages.
4. Language exceptions are specific to programming language and could not be properly used in another programming language.

 

 

See also:




Send feedback... Build date: 2023-09-11
Last edited: 2023-03-07
PRIVACY STATEMENT
The documentation team uses the feedback submitted to improve the EurekaLog documentation. We do not use your e-mail address for any other purpose. We will remove your e-mail address from our system after the issue you are reporting has been resolved. While we are working to resolve this issue, we may send you an e-mail message to request more information about your feedback. After the issues have been addressed, we may send you an email message to let you know that your feedback has been addressed.


Permanent link to this article: https://www.eurekalog.com/help/eurekalog/dll_what_is_exception.php