Line number of exception?
Line number of exception?
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.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Line number of exception?
Perhaps you could post some code. I can't see the problem.
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
>>> try:
... 1/0
... except:
... 1/0
...
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
ZeroDivisionError: division by zero
>>>
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
>>>
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: Line number of exception?
First question: do you know if those builds enabled MICROPY_ENABLE_SOURCE_LINE?
And the question I'm actually trying to get answered:
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?
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.
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.
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/
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/
Re: Line number of exception?
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?
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?