Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   try, except, finally ... where's noexcept? (http://www.programmingforums.org/showthread.php?t=8549)

Sane Feb 23rd, 2006 7:58 PM

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:
    code2


Where 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.

The Dark Feb 24th, 2006 1:28 AM

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:
    code2


or 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).

Arevos Feb 24th, 2006 6:40 AM

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

That should work.

Sane Feb 24th, 2006 5:44 PM

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)

Which obviously didn't work.


All times are GMT -5. The time now is 9:14 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC