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 · ISSUE
Custom exceptions don't support keyword arguments
proposed-closeneeds-info
I have code that looks like:
class MyException(Exception):
def __init__(self, summary, details=None):
self.summary = summary
self.details = details
def error():
raise MyException('oops', details='This is a test')
Calling the error function results in:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "extest.py", line 8, in error
TypeError: function does not take keyword arguments
I think this is mostly a documentation bug. The builtin types documentation has two similar but not quite identical entries:
- User-defined attributes for builtin exceptions are not supported
- Exception.init raises TypeError if overridden and called by subclass
I'm unclear if this is actually just a new expression of one of the existing cases, or if there should be a new entry along the lines of "Keyword arguments on user-defined exceptions are not supported".