← index #1700Issue #3441
Off-topic · high · value 1.788
QUERY · ISSUE

Custom exception cannot be re-raised from called function

openby mbueschopened 2015-12-09updated 2026-03-18
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

closedby larsksopened 2017-11-15updated 2025-06-10
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:

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".

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied