0
Can you use the raise statement outside the except block?
try: num = 5 / 0 except: print("An error occurred") raise ------------------------------------------------------------------ ----------------------------------------------------------------- An error occurred Traceback (most recent call last): File "..\Playground\", line 5, in <module> raise RuntimeError: No active exception to reraise
2 Answers
+ 2
You can use the raise statement anywhere, although if not in an except block you must specify the error (ex: raise or raise TypeError('wrong type')).
In the code you sent with the question, the raise is not indented, which in python means it is not part of the except block.
+ 2
I answered that in the first part of my answer. If you were asking if you can re-raise the same exception outside of the except block, then I don't think so unless you assign it to a variable or something. As I mentioned before, you can call raise anywhere with a new exception.