A way to catch any exception?

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
eradicatore
Posts: 52
Joined: Thu Apr 20, 2017 9:19 pm

A way to catch any exception?

Post by eradicatore » Fri Jun 23, 2017 5:56 am

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

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: A way to catch any exception?

Post by pythoncoder » Fri Jun 23, 2017 8:24 am

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:

Code: Select all

try:
    # something which might raise an OSError
except OSError:
    # Flash an LED
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:
    # lots of code
except:
    pass  # very bad idea
Peter Hinch
Index to my micropython libraries.

eradicatore
Posts: 52
Joined: Thu Apr 20, 2017 9:19 pm

Re: A way to catch any exception?

Post by eradicatore » Fri Jun 23, 2017 11:21 am

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

Damien
Site Admin
Posts: 647
Joined: Mon Dec 09, 2013 5:02 pm

Re: A way to catch any exception?

Post by Damien » Tue Jun 27, 2017 6:27 am

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.

radders
Posts: 7
Joined: Mon Feb 04, 2019 4:41 pm

Re: A way to catch any exception?

Post by radders » Wed Feb 06, 2019 2:15 pm

I've got some code:

Code: Select all

    except OSError as exc:
        if exc.args[0] == uerrno.EWOULDBLOCK: 
            print ('EWOULDBLOCK')
            time.sleep(1)           # short delay, no tight loops
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?

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Detecting when a nonblocking socket would block

Post by pythoncoder » Wed Feb 06, 2019 5:38 pm

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.

radders
Posts: 7
Joined: Mon Feb 04, 2019 4:41 pm

Re: A way to catch any exception?

Post by radders » Mon Mar 11, 2019 1:53 pm

Peter,

I'm getting:

Code: Select all

NameError: name 'errno' isn't defined
Dave

kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: A way to catch any exception?

Post by kevinkk525 » Mon Mar 11, 2019 4:09 pm

You need to import errno.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

radders
Posts: 7
Joined: Mon Feb 04, 2019 4:41 pm

Re: A way to catch any exception?

Post by radders » Wed Mar 20, 2019 2:02 pm

TY. Simple when you know how :lol:

Post Reply