472,364 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,364 software developers and data experts.

how does exception mechanism work?

Sometimes the best way to understand something is to understand the
mechanism behind it. Maybe that is true for exceptions. This is a model
I have right now (which probably is wrong)

1. When a runtime error occurs, some function (probably some class
method) in Python is called behind the scenes.
2. That function goes into a global table having error types in one
column and corresponding flags in another (and probably other columns
used for trace back information). It sets the flag for the error type
that occured.
3. Then the function looks if the error occured inside a try statement
(in the current function or the function that called the current
function, or the one that called that function and so on). If it did
occur inside a try statement it looks if the corresponding exception
handles the occured error type and if so it executes the statements
under the exception clause. The function also resets the type error
flag.
4. If no try statement is found in 3. (or no exception handling the
occured type error) then it is an unhandled error and the Python stops
the execution, prints an error message and resets the global type error
flag)

Is this model correct or wrong? Where can I read about the mechanism
behind exceptions?

Bob

Dec 12 '05 #1
3 2085
bo*******@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.

Good book: "Structure and Interpretation of Computer Programming"
by Abelson and Sussman,

http://mitpress.mit.edu/sicp/

It explains all this stuff (uses a different language from Python, but
principles are similar). Full text is online.
Dec 12 '05 #2
On Mon, 12 Dec 2005, it was written:
bo*******@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'. When the exception-handling machinery is walking the
stack, rather than looking at some concrete stack of exception handlers,
it walks the stack of stack frames (or activation records or whatever you
call them), and for each one, follows the pointer to the relevant method
definition and inspects its exception table. Finally blocks are handled by
putting the finally's code right after the try's code in the normal flow
of execution, then concocting an exception handler for the try block which
points into the finally block, so however the try block finishes,
execution goes to the finally block.

The advantage of this approach over an explicit stack of handlers is that,
although unwinding the stack is perhaps a bit slower, due to having to
chase more pointers to get to the exception table, there's zero work to be
done to set up a try block, and since executing a try is a lot more
frequent than executing a throw-catch, that's a win.

Of course, that's how the conceptual virtual machine does it; real
implementations don't necessarily do that. That said, it is a traditional
superstition in java that a try block is essentially free, which would
suggest that this sort of implementation is common. Indeed, i see no
reason why it wouldn't be - 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.

tom

--
limited to concepts that are meta, generic, abstract and philosophical --
IEEE SUO WG
Dec 13 '05 #3
Tom Anderson <tw**@urchin.earth.li> wrote:
On Mon, 12 Dec 2005, it was written:
bo*******@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.
Dec 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Zachary | last post by:
Hello, I'm relatively new at Python, and am slightly confused about how the try...except mechanism is supposed to work. I am just not sure how it would be used, as it just seems that it is...
7
by: Douglas Peterson | last post by:
Take a look at this code, it looks funny as its written to be as short as possible: -- code -- struct Base { ~Base() { *((char*)0) = 0; } }; struct Derived : public Base
28
by: Scott Brady Drummonds | last post by:
Hi, all, I just got out of a meeting with a team of software developers that I recently joined as they are staffing to create a medium-sized project (potentially all of which will be written in...
3
by: Master of C++ | last post by:
Hi, I am an absolute newbie to Exception Handling, and I am trying to retrofit exception handling to a LOT of C++ code that I've written earlier. I am just looking for a bare-bones, low-tech...
6
by: Kevin Jackson | last post by:
Let's say we log exceptions to the windows application event log using the Exception Management Application Block. Is there a pattern that can be used so the exception is logged only once and not...
3
by: Michael C | last post by:
I'm writing an app on the PDA using C# with .net 1.1. It is all working well except in some cases a try catch is simply ignored and a totally different error is returned. I've got code like below...
11
by: George2 | last post by:
Hello everyone, How do you understand the Bjarne's comments about exception specification? Especially, "not required to be checked across compilation-unit" and "violations will not be caught...
12
by: Ioannis Vranos | last post by:
Perhaps a mechanism can be introduced in the C++0x/1x standard, something simple like defining a function as: void somefunc(void) throw() { // ... }
162
by: Sh4wn | last post by:
Hi, first, python is one of my fav languages, and i'll definitely keep developing with it. But, there's 1 one thing what I -really- miss: data hiding. I know member vars are private when you...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.