Re: Try Finally...

From: L D Blake (not_at_any.adr)
Date: 10/28/04


Date: Thu, 28 Oct 2004 04:47:13 -0400

On 28 Oct 2004 07:59:05 GMT, vbdis@aol.com (VBDis) wrote:

>Im Artikel <2u8158F2706ubU1@uni-berlin.de>, Rob Kennedy <me3@privacy.net>
>schreibt:
>
>>If SysUtils is not used, then system exceptions are not transformed into
>>Delphi exceptions, and so Delphi's try-except blocks don't handle them.
>
>Okay, so one big part of Delphi SEH requires conversions for system exceptions,
>interrupts, assertions and other Delphi exceptions into Delphi SEH structures.

OK lets try and clear some of this up. Structured Exception Handling is a
Windows thing. It is governed by guard frames placed in the program's stack.
These frames are put in place by Delphi's TRY keyword. Each frame contains
(at minimum) the address of an exception handler and a pointer on the stack to
the previous frame.

The exceptions themselves originate from Windows, not Delphi. Delphi cannot
raise an exception... it has to ask windows to do that.

When an exception occurs Windows, not Delphi, walks the stack looking at the
guard frames and, in turn, asking each exception handler if it's interested in
taking care of the problem. This process can be very simple, as in the case
of an Except keyword which basically takes any exception that comes along and
calls it fixed. Or it can become horrificly complicated with handlers
registered only for specific types of exceptions (eg. One that only handles
memory errors).

At the top of the list (because the stack grows downward in memory) is a guard
frame for "unhandled exceptions". If nothing else catches an exception, this
does... and Windows "unhandled exception handler" then closes down the
application. Applications can put in their own unhandled exception handlers
and produce their own shutdowns and error messages... which Delphi does.

In Delphi these guard frames are created at the beginning of a TRY block and
removed at the beginning of the Finally or Except block. Any exception that
does not occur within the Try block is going to end up in the Unhandled
Exception Handler. And, since each program has it's own stack space,
exceptions from other applications never get into places they're not supposed
to.

It's rather simple... but horridly complex at the same time.

When an exception comes along, Windows walks the stack looking for a handler.
Delphi catches it and does it's thing making that exception into the objects
that you all like to play with.

The hard part for us to understand is that when we do something that causes an
exception, the exception is not created in Delphi (or whatever language we are
using), it originates in the operating system. What the language does with an
active exception is entirely up to the language (and by extension, us).

>Consequently a replacement SEH should specify what it supports, at least:
>- Delphi version
>- which kinds of error conditions (by triggering Delphi exception handling)
>- exceptions during exception handling (in exception handling code)
>- debugging
>- possibly non-standard behaviour of Except and Finally

Yeah well... that's rather a dead horse now, isn't it?

-----
Laura
http://www.start.ca/users/ldblake



Relevant Pages

  • Re: D9 rant...
    ... Delphi 2005 Update 1 Fixes ... used they conflict with the Delphi types Pointer and Exception. ... * Access violation occurs in dccil90.dll on second compile in the ... package lists. ...
    (borland.public.delphi.non-technical)
  • Re: Been a slice...
    ... File system changes don't sound like errors, ... > nicely with RAD and VCL developement. ... involves objects during exception handling. ... > causing too much havoc with the basic Delphi functions... ...
    (comp.lang.pascal.delphi.misc)
  • Re: Exception Handling...
    ... just windows. ... remapped from windows codes to delphi codes to get ... them on to the "unhandled exceptions" handler. ... >exception handler be located. ...
    (comp.lang.pascal.delphi.misc)
  • Re: Cargo cults and the taxonomy of incompetence
    ... on E: exception do DecideRetry; ... differently in DecideRetry by forking on different Exception descendents, ... programmer who hated Delphi, why they were going to do it this way, he said it ... about programming, so they read no programming books, magazines or websites ...
    (borland.public.delphi.non-technical)
  • Re: Ok.. live and learn.
    ... > Delphi has embedded their SEH hadlers into the System unit. ... > short of re-writing the compiler we are indeed stuck with it. ... It's clear that you don't like Delphi exception handling and want ... Object Pascal programs to use Windows SEH. ...
    (comp.lang.pascal.delphi.misc)