A way to catch any exception?
-
- Posts: 52
- Joined: Thu Apr 20, 2017 9:19 pm
A way to catch any exception?
I notice that when my python has an error, I get the REPL prompt on serial. Is there a way to do something in code when this exception happens, like blink an LED for example? Where is this REPL prompt coming from? Is it embedded in the micropython binary to go to that for unhandled exceptions?
Justin
Justin
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: A way to catch any exception?
This is a query about the Python language rather than anything specific to MicroPython. There are plenty of books and online tutorials which can help you and I suggest getting familiar with the language on a PC. In essence if your program stops for any reason (e.g. running to completion or throwing an exception), you get back to the REPL. To catch exceptions you use code like this:
It is possible to catch and ignore any exception as below, but it is bad practice because syntax errors will be ignored so you'll never find your bugs.
Code: Select all
try:
# something which might raise an OSError
except OSError:
# Flash an LED
Code: Select all
try:
# lots of code
except:
pass # very bad idea
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
-
- Posts: 52
- Joined: Thu Apr 20, 2017 9:19 pm
Re: A way to catch any exception?
Ok, thanks! Yea, I was mainly asking about your comment "if it stops for any reason". I should have mentioned I'm already familiar with the try/except format, but was looking for a "global" version to do something if my board dies this way and I didn't have the try/except around the culprit
Re: A way to catch any exception?
When a script finishes (either it runs to the end or has an uncaught exception) then the MicroPython runtime will go to the REPL.
If you want to catch any exception and handle that (eg reset the device) then do as suggested above, run your outer-most code in a try/except block, and do the error handling in the except.
If you want to catch any exception and handle that (eg reset the device) then do as suggested above, run your outer-most code in a try/except block, and do the error handling in the except.
Re: A way to catch any exception?
I've got some code:
which came from a sample on a forum, but I get errors at the uerrno line.
What is the correct implementation for this port of MicroPython, please?
Code: Select all
except OSError as exc:
if exc.args[0] == uerrno.EWOULDBLOCK:
print ('EWOULDBLOCK')
time.sleep(1) # short delay, no tight loops
What is the correct implementation for this port of MicroPython, please?
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Detecting when a nonblocking socket would block
I think the EWOULDBLOCK error dates back to an older version of Python. The correct line (for Python 3.4 and hence for MicroPython) is:
Code: Select all
if err == errno.EAGAIN:
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
-
- Posts: 969
- Joined: Sat Feb 03, 2018 7:02 pm
Re: A way to catch any exception?
You need to import errno.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode
Re: A way to catch any exception?
TY. Simple when you know how