Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Conditional except: blocks

Reply
Thread Tools

Conditional except: blocks

 
 
Robert Brewer
Guest
Posts: n/a
 
      01-02-2004
I don't have a quesiton or problem, I just figured some of you would
enjoy the somewhat bizarre logic of the snippet I just wrote for a CGI
request dispatcher:

try:
return nextHandler(args)
except (None, Exception)[self.trapErrors], exc:
if page == u'-Error':
raise exc
else:
return self.handle_error(exc, scriptWithPath)

If self.trapErrors is True, all exceptions (which subclass Exception)
get trapped, otherwise, no exceptions are trapped. Spiffy.


FuManChu

 
Reply With Quote
 
 
 
 
Aahz
Guest
Posts: n/a
 
      01-02-2004
In article <(E-Mail Removed)>,
Robert Brewer <(E-Mail Removed)> wrote:
>
>I don't have a quesiton or problem, I just figured some of you would
>enjoy the somewhat bizarre logic of the snippet I just wrote for a CGI
>request dispatcher:
>
>try:
> return nextHandler(args)
>except (None, Exception)[self.trapErrors], exc:
> if page =3D=3D u'-Error':
> raise exc
> else:
> return self.handle_error(exc, scriptWithPath)
>
>If self.trapErrors is True, all exceptions (which subclass Exception)
>get trapped, otherwise, no exceptions are trapped. Spiffy.


That's pretty sick. If you really need to do that, here's a more
Pythonic approach:

# This goes at beginning of program
# or maybe at class/instance initialization
if trapErrors:
exceptTuple = (Exception,)
else:
exceptTuple = (None,)

# some time later
try:
return nextHandler(args)
except exceptTuple, exc:
# Handle exception
--
Aahz ((E-Mail Removed)) <*> http://www.pythoncraft.com/

Weinberg's Second Law: If builders built buildings the way programmers wrote
programs, then the first woodpecker that came along would destroy civilization.
 
Reply With Quote
 
 
 
 
sdd
Guest
Posts: n/a
 
      01-02-2004
Robert Brewer wrote:
> try:
> return nextHandler(args)
> except (None, Exception)[self.trapErrors], exc:
> if page == u'-Error':
> raise exc

raise # re-raises the same error with the full traceback.
> else:
> return self.handle_error(exc, scriptWithPath)


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
? ELSE Conditional Comment / Using Conditional Comments Inside Other Tags To Comment Out Attributes Alec S. HTML 10 04-16-2005 02:21 AM
Conditional html blocks Richard Frenkel ASP .Net 2 01-10-2005 07:09 PM
procs/blocks - blocks with procs, blocks with blocks? matt Ruby 1 08-06-2004 01:33 AM
Help writing conditional blocks estafford ASP .Net 2 08-19-2003 06:58 PM



Advertisments