Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python reraise/recatch exception

I would like to know if it is possible in python to raise an exception in one except block and catch it in a later except block. I believe some other languages do this by default.

Here is what it would look like"

try:
   something
except SpecificError as ex:
   if str(ex) = "some error I am expecting"
      print "close softly"
   else:
      raise
except Exception as ex:
   print "did not close softly"
   raise

I want the raise in the else clause to trigger the final except statement.

In actuality I am not printing anything but logging it and I want to log more in the case that it is the error message that I am not expecting. However this additional logging will be included in the final except.

I believe one solution would be to make a function if it does not close softly which is called in the final except and in the else clause. But that seems unnecessary.

like image 982
Aaron Robinson Avatar asked Jun 09 '11 21:06

Aaron Robinson


People also ask

How do you reraise an exception in Python?

Python Language Exceptions Re-raising exceptions In this case, simply use the raise statement with no parameters. But this has the drawback of reducing the exception trace to exactly this raise while the raise without argument retains the original exception trace.

How do you raise an exception in an except block in Python?

In Python, exceptions can be handled using a try statement. The critical operation which can raise an exception is placed inside the try clause. The code that handles the exceptions is written in the except clause. We can thus choose what operations to perform once we have caught the exception.

How do I print an increase exception?

To catch and print an exception that occurred in a code snippet, wrap it in an indented try block, followed by the command "except Exception as e" that catches the exception and saves its error message in string variable e . You can now print the error message with "print(e)" or use it for further processing.

What is an raising exception in Python with example?

When handling an exception, you may want to raise another exception. For example: def division(a, b): try: return a / b except ZeroDivisionError as ex: raise ValueError('b must not zero') Code language: Python (python) In the division() function, we raise a ValueError exception if the ZeroDivisionError occurs.


2 Answers

What about writing 2 try...except blocks like this:

try:
    try:
       something
    except SpecificError as ex:
       if str(ex) == "some error I am expecting"
          print "close softly"
       else:
          raise ex
except Exception as ex:
   print "did not close softly"
   raise ex
like image 66
badzil Avatar answered Sep 21 '22 14:09

badzil


Only a single except clause in a try block is invoked. If you want the exception to be caught higher up then you will need to use nested try blocks.

like image 22
Ignacio Vazquez-Abrams Avatar answered Sep 20 '22 14:09

Ignacio Vazquez-Abrams