![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programming Guru
![]() |
try, except, finally ... where's noexcept?
I need something that will allow code encased in "try", to not be excepted by a specific error message.
The deprecated HTTP redirect in cherrypy raises an error called "cherrypy._cperror.HTTPRedirect", which messes up my debug decorator. I need to be able to go: try, noexcept cherrypy._cperror.HTTPRedirect:
code1
except:
code2Where code2 will not be executed if an HTTPRedirect error occurs, but it will with any other error. Any solutions? The obvious being not to use an external redirect. |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 852
Rep Power: 4
![]() |
Do you want to trap the exception and not perform code2? I would think you would do something like:
try:
code1
except cherrypy._cperror.HTTPRedirect:
/* Do nothing*/
except:
code2or do you want to completely ignore the error and continue on with the rest of code1? In this case, you might have to have try ... except around each inidivual bit of code that might cause the exception (or put it in a function that has the try ... except in it). |
|
|
|
|
|
#3 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
HTTPRedirect is an exception that isn't an error. I can see why this would mess up your debug decorator, Sane; you want your decorator to log "normal" exceptions, but to ignore HTTPRedirect exceptions, because they need to be handled by CherryPy.
The solution is to catch the HTTPRedirect exception, and then to immediately raise it again: try:
code 1
except cherrypy._cperror.HTTPRedirect, e:
raise cherrypy._cperror.HTTPRedirect, e
except:
code 2 |
|
|
|
|
|
#4 |
|
Programming Guru
![]() |
OOhh! Raise it again! Gah! I should have thought of that... It's so obvious! Thanks!
This is what I had tried before try:
return func(*args, **kwargs)
except cherrypy._cperror.HTTPRedirect:
pass
except:
return serveError(func.func_name, func.func_code, exc_info()[0], *args, **kwargs) |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|