[Tutor] Universal error handler

Alan Gauld alan.gauld at freenet.co.uk
Tue Jun 20 19:36:53 CEST 2006


> Just started learning Python, and I was wondering if there is a way 
> to catch an error or errors (like EOFError) whenever it occures 
> during the script? The script (the program) wouldn't have any 
> handling for that error itself, but a seperate block would handle 
> that at the start or the end of the file.

Thats pretty much a description of try/except handling style but...

> For a simple 200 line script I made the whole thing into a function, 
> and at the end put:
>
> def main():
>     # Do the program itself..
>
> # The very end
> try:
>     main()
> except EOFError:
>     # Tell something went horribly wrong

> I find this way of doing it disfunctional when having lots of code.

It looks a lot like what you asked for! What do you find
disfunctional? Is it the call to the main function?

You could just write the code:

try:
    # contents of main here
except EOFError:
    # error code here

But I assume you realised that, son what exactly do you
find amiss? I'm a wee bit confused.

But your post worked - it got a response, cxongratulations,
welcome to the community :-)

Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list