[Tutor] Exception Handling
spir
denis.spir at free.fr
Mon Dec 29 16:57:44 CET 2008
On Mon, 29 Dec 2008 09:10:45 -0000
"Alan Gauld" <alan.gauld at btinternet.com> wrote:
>
> "bob gailer" <bgailer at gmail.com> wrote
>
> > Also IMHO it is bad design to put a lot of code inside a try block.
> > In this case the user might make a mistake on day and then is forced
> > to reenter the year and month!
>
> Obviously there is no absolute rule here but I disagree.
> One of the biggest advantages of try/except error handling is
> that it keeps the mess of handling errors out of the main logic
> of the code. This has two effects:
> 1) Code that is much easier to read
> 2) a lot less error handling code
>
> Using "big bite" try/except does mean the except clauses
> become more complex if you want to find out exactly which
> line caused the error, as in the example above, but it can
> be done by examining the traceback, but in general I much
> prefer to wrap logical blocks of code using try/except rather
> than only one or two lines
I often use the else clause of try...except. This allows putting only the
minimum problematic code lines inside the try block, which is good both for
legibility andto avoid catching unexpected errors. The else will be executed
only in case of no exception:
try:
problematic_instructions
except ExceptionType, exception:
expected_exception_handler
else:
further_standard_process
denis
------
la vida e estranya
More information about the Tutor
mailing list