how does exception mechanism work?

Ben Hutchings ben-public-nospam at decadentplace.org.uk
Tue Dec 13 00:11:49 EST 2005


Tom Anderson <twic at urchin.earth.li> wrote:
> On Mon, 12 Dec 2005, it was written:
>
>> bobueland at yahoo.com writes:
>>
>>> Is this model correct or wrong? Where can I read about the mechanism 
>>> behind exceptions?
>>
>> Usually you push exception handlers and "finally" clauses onto the 
>> activation stack like you push return addresses for function calls. When 
>> something raises an exception, you scan the activation stack backwards, 
>> popping stuff from it as you scan and executing "finally" clauses as you 
>> find them, until you find a handler for the raised exception.
>
> That varies an awful lot, though - AIUI, in java, the catch blocks are 
> specified sort of in the same place as the code; a method definition 
> consists of bytecode, a pile of metadata, and an exception table, which 
> says 'if an exception of type x happens at a bytecode in the range a to b, 
> jump to bytecode c'.
<snip>
> i think the push-a-handler style seen in C/C++ 
> implementations is only necessary because of the platform ABI, which 
> doesn't usually mandate a standard layout for per-function metadata.

Most platform ABIs have only covered C, which doesn't have any
exception-handling (though POSIX threads sort of requires it).  The
only platform ABIs I'm aware of that require pushing handlers are the
Win32 ABIs.  I believe the technique of pushing exception handler
records on the stack was used because it was relatively obvious and
initially not too expensive (since few exception handlers were used).

FWIW, most C++ implementations now use range tables, as you described.
So do the Win64 ABIs and the Unix/Linux IA64 ABI.

-- 
Ben Hutchings
Klipstein's 4th Law of Prototyping and Production:
                                    A fail-safe circuit will destroy others.



More information about the Python-list mailing list