Page 1 of 1
Get full stack trace in try: catch:
Posted: Sun Jul 24, 2016 6:12 pm
by mbrej
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
Re: Get full stack trace in try: catch:
Posted: Sun Jul 24, 2016 8:38 pm
by dhylands
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.
Re: Get full stack trace in try: catch:
Posted: Mon Jul 25, 2016 8:21 am
by deshipu
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
Re: Get full stack trace in try: catch:
Posted: Wed Jul 27, 2016 11:30 am
by pfalcon
And MicroPython has documentation with search facility:
http://docs.micropython.org/en/latest/u ... ea=default . (And micropython-lib, which provides traceback module.)
Re: Get full stack trace in try: catch:
Posted: Wed Jul 27, 2016 3:24 pm
by dhylands
Sweet - not sure why I didn't know about that.
Re: Get full stack trace in try: catch:
Posted: Sat Jul 30, 2016 4:56 pm
by mbrej
Many thanks, it works great.
Re: Get full stack trace in try: catch:
Posted: Tue Mar 27, 2018 6:37 pm
by semireg
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.
Re: Get full stack trace in try: catch:
Posted: Tue Mar 30, 2021 8:28 am
by aleskeyfedorovich
the traceback module does not work because sys.exc_info is not available [link](
https://github.com/micropython/micropython/issues/5110)