Line number of exception?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
mwm
Posts: 36
Joined: Wed Aug 09, 2017 3:52 pm

Line number of exception?

Post by mwm » Sat Aug 19, 2017 3:41 pm

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.

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

Re: Line number of exception?

Post by pythoncoder » Mon Aug 21, 2017 6:10 am

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
>>> 
Peter Hinch
Index to my micropython libraries.

mwm
Posts: 36
Joined: Wed Aug 09, 2017 3:52 pm

Re: Line number of exception?

Post by mwm » Mon Aug 21, 2017 9:25 pm

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?

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

Re: Line number of exception?

Post by pfalcon » Tue Aug 22, 2017 6:29 am

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.
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/

mwm
Posts: 36
Joined: Wed Aug 09, 2017 3:52 pm

Re: Line number of exception?

Post by mwm » Tue Aug 22, 2017 1:04 pm

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?

vodkawasserfall
Posts: 4
Joined: Tue Feb 02, 2021 3:10 am

Re: Line number of exception?

Post by vodkawasserfall » Fri Jul 02, 2021 7:13 pm

Bump

Post Reply