views:

95

answers:

8

Possible Duplicate:
Does a exception with just a raise have any use?

Is there any value to re-raising an exception with no other code in between?

try:
  #code
except Exception:
  raise

I was recently looking through some code and saw a few blocks like these with nothing extra in the except block but another raise. I assume this was a mistake and poor decision making, am I right?

+2  A: 

I am not able to come up with something useful, other than to keep it as a placeholder for later insertion to catch useful exceptions.

It kind of avoids re-indenting the code, when you want to include the "try .. except.." blocks later on.

pyfunc
+1  A: 

Yes, this is usually a bad practice. The only (somewhat) correct usage I've seen of this pattern was before VB.NET had a Using construct available. Usage looked something like:

Dim myResource As DisposableService

Try
   myResource = New DisposableService()
   ' This might throw an exception....
   myResource.DoSomething()
Catch
  Throw
Finally
  ' Perform cleanup on resource
  If Not myResource Is Nothing Then
      myResource.Dispose()
  End If
End Try

Other than that, I really can't think of a good use case for this sort of thing.

DanP
Hah. Funnily we both have vb.net examples!
Josh Smeaton
Funnily; or sadly? ;)
DanP
Couldn't you just leave out the catch-throw sequence?
Yuliy
@Yuliy: No, picture you were working with something that implemented IDisposable; the only 'fail safe' way to perform the cleanup operation on it was to do it in the Finally block. Thankfully this is now handled with the Using construct.
DanP
I've updated my example to show more context.
DanP
@DanP: not sure that answers his question.. can't you have `try...finally` without a `catch...throw` in there?
Claudiu
ohhhhh...gotcha...thanks for the clarification
DanP
A: 

I've seen similar code before in a (set of) horrible VB.NET projects. Either the intent was to catch and log exceptions, without ever coming back to finish the logging, or they heard "you must catch exceptions", implemented this functionality, and someone else decided it should just re-raise.

There is no benefit to the above code.

Josh Smeaton
A: 

Typically in a try-catch model, any uncaught exception will automatically be thrown (raised?). Catching the exception only to re-throw it may be in the spirit Allman style coding, but serves no functional purpose.

Matt H.
A: 

Uh, Imagine

def something(a,b):
   try:
      // do stuff
   except SomethingSpecificToThisFunction:
      //handle
   except: //Everything else, should likely be handled somewhere else
      raise


try:
   something("a","b")
except e:
   Log(e)

Then again its default behaviour anyways, so might just want to leave it out

Robus
+2  A: 

Example built on this question. If there's some other except's in the try block, it can be used to filter the exceptions, but alone it's pointless.

class FruitException(Exception): pass

try:
    raise FruitException
except FruitException:
    print "we got a bad fruit"
    raise
except Exception:
    print "not fruit related, irrelevant."
Nick T
A: 

There are some approaches with such technics in multithread enviroment. For example to throw something to upper stack level.

Andrey Gubarev
+1  A: 

sometimes it useful let me give you a real example that i did i my work :

this was is in a decorator that wrap func : so basically what i have wanted is to re-raise the error that i catched when i called the function func so that the decorator don't change the behavior of the function func, because when func raise an exception the exception are send to the GUI so that an error message can pop up to the user,

and for the try except i use it because i want to execute the code in finally even if an exception is raised

try:
     result = func(self, *args, **kws)
     return result
except Exception, ex:
     # If an exception is raised save it also.
     logging_data['message'] = str(ex)
     logging_data['type'] = 'exception'

     # Raise the error catched here so that we could have
     # the same behavior as the decorated method.
     raise
finally:
     # Save logging data in the database
     ....

hope this will help to understand the use of re-raise

singularity