Any value when catching an exception and immediately repeating it?

Possible duplicate:
Does the exception use only a raise?

Is there any value for re-throwing an exception without another code between them?

try:
  #code
except Exception:
  raise

Recently, I looked at some code and saw several blocks like these, nothing more in the block except the block, and another raise. I assume it was a mistake and poor decision making, am I right?

+3
source share
8 answers

() VB.NET. , , , , " ", , - , .

.

+2

- , , .

, "try.. except.." .

+2

. try except, , .

class FruitException(Exception): pass

try:
    raise FruitException
except FruitException:
    print "we got a bad fruit"
    raise
except Exception:
    print "not fruit related, irrelevant."
+2

, . () , , , VB.NET Using. :

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

, .

+1

, , :

, func: , , - , , func, func, , func , GUI, ,

try, , , , ,

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

, re-raise

+1

, try-catch (?). Allman , .

0

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)

, - ,

0

​​ . , - .

0

Source: https://habr.com/ru/post/1769632/


All Articles