If I test the code shown below I don't get the exception I am expecting, I get a MemoryError instead of MyException.
Is it possible to use exceptions in ISRs? if so is there some trick way of handling them.
(the additional lines in the code are to allow me to (A) test it on a PC and also (B) demonstrate on the pyboard that it is the exception in the callback causing the problem and not the print statement)
>>> Uncaught exception in ExtInt interrupt handler line 3
Traceback (most recent call last):
File "main.py", line 10, in f
File "main.py", line 8, in f
MemoryError: memory allocation failed, heap is locked
Code: Select all
class MyException(Exception):
def __init__(self):
pass
def f():
try:
raise MyException()
except MyException as e:
print('my exception function f')
def g():
print('function g')
try:
import pyb
import micropython
micropython.alloc_emergency_exception_buf(100)
sw=pyb.Switch()
sw.callback(f)
except ImportError:
print('pyb not available simlate instead')
f()
g()