-
Hi! I have been stressing with this issue I am facing about my defined exception classes. As an example:
I try to raise and handle it:
I get the following error:
I can not get my head around why is this issue happening. I tried searching but nothing fruitful came out. In fact, as I see similar post in the old micropython blog, it seemed it should work. I tried in wokwi simulator and it worked fine. Although, the simulator was using esp32 with micropython 1.19.1. My board is a esp01 and micropython version is 1.20.0 I'd appreciate any help on this matter! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 10 replies
-
You can try this: class myexception(Exception):
def __init__(self, message):
print('My Exception:')
super().__init__(' '+message) With that you can do: >>>
MPY: soft reboot
MicroPython v1.22.0-preview.1.g523284777.dirty on 2023-10-07; ESP8266 module with ESP8266
>>>
paste mode; Ctrl-C to cancel, Ctrl-D to finish
=== class myexception(Exception):
=== def __init__(self, message):
=== print('My Exception:')
=== super().__init__(' '+message)
===
=== try:
=== raise myexception('not ok that')
=== except Exception as e:
=== print(e)
===
My Exception:
not ok that
>>> |
Beta Was this translation helpful? Give feedback.
-
@ShocKwav3 there's something about the way you're importing the exception from your
?? Or perhaps something in The error you listed was:
This means that it is trying to find an except block for an active This is why it works when you run it directly (as what you're donig is definitely supported as @shariltumin and @peterhinch have pointed out), but not when you import the exception. |
Beta Was this translation helpful? Give feedback.
-
As Jim pointed out earlier, your import statement may be in error. Since you have a subdirectory called "exceptions" and you put the wifiexception.py file there, please try. from exceptions.wifiexception import wifiexception The above tries to import wifiexception from "./exceptions/wifiexception.py". I will do it differently. I will have a file "my_exceptions.py". I will declare all my all my exception classes in it. My import will be from my_exceptions import myexception1, myexception2 It can be confusing to name a module file and a class in the modules with the same name. |
Beta Was this translation helpful? Give feedback.
@ShocKwav3 there's something about the way you're importing the exception from your
exceptions
directory... Back in the original post, the error is a bit subtle, but tells you that instead of importingSomeExceptionClass
, you imported a module, but named itSomeExceptionClass
. Perhaps you wrote??
Or perhaps something in
exceptions/__init__.py
is doing something strange?The error you listed was:
This means that it is trying to find an except block for an active
TypeError
, and it's trying to match it against something (S…