How are exceptions actually implemented in assembly?

Jason Orendorff jason at jorendorff.com
Fri Jan 25 01:46:40 EST 2002


Bengt Richter wrote:
> I am not speaking as compiler developer, but I imagine that a compiler
> could generate information stored in static tables that describe 
> try blocks
> and their associated exception handlers, to support unwinding the 
> stack and
> finding an exception handler for a raised exception.
> 
> These tables would only be looked at from the raiseException 
> routine, and would
> have no time overhead in the normal execution path. [...]
> I'd be surprised if it's not already being done in some 
> form somewhere.

Yep.  Someone mentioned that this is how exceptions work in GNU C++.
For C++, I believe this is clearly the best way to do it; and if
(as the same someone also mentioned) MSVC++ does it some other way,
it's probably to meet some weird backwards-compatibility requirement.

Not sure how well this applies to Lisp-like languages.
I imagine it's similar.

> BTW, the above is for compiling to machine language. I'm not sure 
> how it might translate into terms of byte code.

It can work just the same way.  Java bytecode files have static
exception-handling tables.  A Java JIT translates these to...
well, whatever it wants.  (But typically, I imagine, it converts
it to another static table that points at the generated machine
code.)

Python's currently written in relatively clean ANSI C.
Everything the Python interpreter does, it does through the ANSI C
abstraction of the underlying machine - which doesn't include any
sort of stack inspection, much less stack unwinding.

For Python to do sophisticated exception handling would mean
a pretty serious departure.  It's unlikely.

## Jason Orendorff    http://www.jorendorff.com/




More information about the Python-list mailing list