QUERY · ISSUE
Custom exception cannot be re-raised from called function
docs
Consider this code:
class MyEx(Exception): pass
def do_raise():
raise
try:
raise MyEx()
except MyEx:
try:
do_raise()
except MyEx:
print("Ok")
else:
print("not Ok")
On MicroPython it fails with
Traceback (most recent call last):
File "/home/mb/test.py", line 12, in <module>
File "/home/mb/test.py", line 10, in <module>
File "/home/mb/test.py", line 4, in do_raise
RuntimeError: No active exception to reraise
Things I noticed:
If the raise is done directly in the handler instead of do_raise, it works correctly (prints Ok).
If the exception is Exception instead of MyEx, it also works correctly.
CANDIDATE · PULL REQUEST
vm: Save current active exception on opening new try block.
Required to reraise correct exceptions in except block, regardless if more
try blocks with active exceptions happen in the same except block.
P.S. This "automagic reraise" appears to be quite wasteful feature of Python
- we need to save pending exception just in case it might be reraised.
Instead, programmer could explcitly capture exception to a variable using
"except ... as var", and reraise that. So, consider disabling argless raise
support as an optimization.