views:

51

answers:

2

I am running an IronPython script inside a c# application, i am catching exceptions within the script and i wish to find out the script line at which the exception is thrown. This has to be done while the script is running ie. i do not wish the script to terminate in order to print the exception.

Is this even possible?

+1  A: 

If inspect is working as expected under IronPython (not really sure) this could do the trick:

import inspect

filename, linenum, funcname = inspect.getframeinfo(inspect.currentframe())[:3]
print linenum

Edit: alternate solution:

import sys

frame = sys._getframe()
print frame.f_lineno
ChristopheD
I can't do that, when i 'import inspect' i get a c# exception 'No module named inspect'.Is there another way?
LoudNPossiblyRight
Added an alternate solution
ChristopheD
that didnt work either but thanks for the effort, this seems to be an irionpython limitation
LoudNPossiblyRight
A: 

Haven't tried this on ironpython but:

import traceback

try:
    # something that raises exception
except YourException, _:
    traceback.print_exc()

This should show you the stack trace of the place where the exception was raised. You can also do other stuff than just print, like print to string, or get the stack frames. The traceback module documentation will tell you more.

Mattias Nilsson