Page 1 of 1
Line number of exception?
Posted: Sat Aug 19, 2017 3:41 pm
by mwm
Is there any way to get the line number an exception occurred on in an except: clause? In CPython, I get it from the traceback returned by sys.exc_info(). I don't see exc_info in micropython's sys, or any way to get the traceback. I did see a define to disable line numbers in the build, so I'm half expecting they simply aren't available from python.
Re: Line number of exception?
Posted: Mon Aug 21, 2017 6:10 am
by pythoncoder
Perhaps you could post some code. I can't see the problem.
Code: Select all
$ upython
MicroPython v1.8.7-1080-g394c536-dirty on 2017-08-20; linux version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> try:
... 1/0
... except:
... 1/0
...
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
ZeroDivisionError: division by zero
>>>
Saving the same code as rats43.py:
Code: Select all
$ upython
MicroPython v1.8.7-1080-g394c536-dirty on 2017-08-20; linux version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> import rats43
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "rats43.py", line 4, in <module>
ZeroDivisionError: division by zero
>>>
Re: Line number of exception?
Posted: Mon Aug 21, 2017 9:25 pm
by mwm
First question: do you know if those builds enabled MICROPY_ENABLE_SOURCE_LINE?
And the question I'm actually trying to get answered:
Is there any way to get the line number an exception occurred on in an except: clause?
Re: Line number of exception?
Posted: Tue Aug 22, 2017 6:29 am
by pfalcon
A line number alone won't get you far. Consider that you have 10 source files, how much information "exception is at line 15" gives you?
And how to capture a complete stacktrace, you can find in the documentation:
http://docs.micropython.org/en/latest/p ... _exception .
Many other questions which may arise when using MicroPython can be answered by searching/reading the documentation too.
Re: Line number of exception?
Posted: Tue Aug 22, 2017 1:04 pm
by mwm
Yes, but the documentation didn't answer my question either. That's why I asked.
So the only way to get information about an exception is to parse the traceback and how it doesn't change in the next revision?
Re: Line number of exception?
Posted: Fri Jul 02, 2021 7:13 pm
by vodkawasserfall
Bump