I want a try-block such that any exception raised inside of thetry-block goes unhandled. This is so that I can write a try block in preparation for the future. Some day, I will write in some meaningful error handling. However, I don't have real except statements yet. The following sort of works, but is ugly
_ = type("", (Exception,), dict())
try:
lizard = [1, 2, 3]
y = z + w
print(lizard[983])
except _:
print("I hope this string never prints")
Skip the except clause altogether. A try statement needs at least one except clause or a finally clause, which executes whether or not you catch an exception.
try:
lizard = [1, 2, 3]
y = z + w
print(lizard[983])
finally:
pass
The finally clause won't actually execute any code, and does not affect the control flow of your code in any way; it just injects a no-op before you leave the try statement, whether by successfully completing the code or by raising an uncaught exception.
Once you start adding except clauses, you can either remove the finally clause or leave it in place.
(A deleted answer catches and immediately reraises any exception, which is also fine IMO:
try:
...
except Exception:
raise
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With