Try, except...retry?

Edvard Majakari edvard+news at majakari.net
Mon Nov 17 07:45:00 EST 2003


Alex Martelli <aleax at aleax.it> writes:

> If this is the case, then, if you make it a habit to program like 
> this, it WILL eventually cost you some unpredictable but likely 
> large amount of debugging time.  A bug will be introduced during
> a refactoring of function 'process', or an 'x' will be generated
> that is inappropriate for the function, etc, etc.  The bug will
> be swallowed and hidden by the too-wide "except" clause.  Unit

How about

class xmlParseException(Exception): pass
class xmlSemanticsException(Exception): pass
class fileReadException(Exception): pass

    try:

        process()
        process2()
        ...lots of code...

    except xmlSemanticsException, e: 
        handle_xml_semantics_errors() 
    except xmlParseException, e:
        handle_xml_parsing_errors() 
    except fileReadException, e:
        handl_file_read_errors()

At least I find it easier to group possibly-exceptions-raising code
separately, inherit specific exceptions from class Exception using the
most simple method (ie. derived class contains only pass as body). Now it
is not that hard to pinpoint where the exception occurred, because there
is custom exception for each type of error situation, and the parameter e
of course contains appropriate error message which even further helps to
pinpoint the problem. 

> think that unit tests are too much trouble, not "easy to understand"
> enough, or the like... -- or else, worse!!!, wrong but semi-plausible
> results will come out of your program...!).  The best wish I can
> make is that the resulting utter waste of hours, days, whatever,
> will fall on the head of whoever IS responsibile for promoting or
> using this wrong structure, and not on some poor hapless maintainer
> coming later onto the scene of the crime.

...but of course, unit tests should be used whenever possible :)

-- 
# Edvard Majakari		Software Engineer
# PGP PUBLIC KEY available    	Soli Deo Gloria!

$_ = '456476617264204d616a616b6172692c20612043687269737469616e20'; print
join('',map{chr hex}(split/(\w{2})/)),uc substr(crypt(60281449,'es'),2,4),"\n";




More information about the Python-list mailing list