Get full stack trace in try: catch:

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
mbrej
Posts: 6
Joined: Mon May 23, 2016 4:03 pm

Get full stack trace in try: catch:

Post by mbrej » Sun Jul 24, 2016 6:12 pm

Hi, is there any way to get a stack trace while in the 'except' part of a try,except?

The best I have got is the error message using:

Code: Select all

except Exception as e:
	text = str(e)
That however just displays 'MemoryError: memory allocation failed, heap is locked,' while ideally I would line the line number and file it occurred in too.

Thanks, Matt

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Get full stack trace in try: catch:

Post by dhylands » Sun Jul 24, 2016 8:38 pm

The best way is to re-raise the error and let it be printed by the C-code.

Perhaps catch the MemoryError and re-raise.

I know we've discussed this, but currently I don't think it can be done from python.

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: Get full stack trace in try: catch:

Post by deshipu » Mon Jul 25, 2016 8:21 am

CPython has a "traceback" module that handles this. Maybe it would make sense to have something similar. https://docs.python.org/2/library/traceback.html

pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: Get full stack trace in try: catch:

Post by pfalcon » Wed Jul 27, 2016 11:30 am

And MicroPython has documentation with search facility: http://docs.micropython.org/en/latest/u ... ea=default . (And micropython-lib, which provides traceback module.)
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Get full stack trace in try: catch:

Post by dhylands » Wed Jul 27, 2016 3:24 pm

Sweet - not sure why I didn't know about that.

mbrej
Posts: 6
Joined: Mon May 23, 2016 4:03 pm

Re: Get full stack trace in try: catch:

Post by mbrej » Sat Jul 30, 2016 4:56 pm

Many thanks, it works great.

User avatar
semireg
Posts: 14
Joined: Mon Dec 18, 2017 3:53 pm

Re: Get full stack trace in try: catch:

Post by semireg » Tue Mar 27, 2018 6:37 pm

Where is the traceback module? I looked at the search URL above but didn't see any reference to a dedicated library.

In the meantime, this works for me:

Code: Select all

    except Exception as e:
        log.critical('--- Caught Exception ---')
        import sys
        sys.print_exception(e)
        log.critical('----------------------------')
Edit: This looks like what is being described. https://github.com/micropython/micropyt ... aceback.py

Unfortunately, it just looks like a wrapper to sys.print_exception. What I would like is to have access to the call stack, an array of __name__ would suffice.

aleskeyfedorovich
Posts: 28
Joined: Mon Aug 27, 2018 4:43 pm

Re: Get full stack trace in try: catch:

Post by aleskeyfedorovich » Tue Mar 30, 2021 8:28 am

the traceback module does not work because sys.exc_info is not available [link](https://github.com/micropython/micropython/issues/5110)

Post Reply