Python Forum

Full Version: line number of exception
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I can get error message printed out how to get the line number at which exceptoin occurred:

try:
    stat = callablePy(mainArgv, instGlobalConfig)
except Exception as errMsg:
    printErr("Exception occurred. ")
    printErr(str(errMsg))
    printErr(str(errMsg.__class__))
    stat = RET_EXCEPT

i am calling another python module from main module as follows, but exception prints out the main module's file and line no. I need to get the called module's file name occurrence and line no how do i do that?
print "importing " + file1
module = __import__(file1)

try:
    callable = getattr(module, "main")
    res = callable( [str(file1), "asd"] )
except Exception as errMsg:
    print "exception!:"
    print errMsg
    print errMsg.__class__
    obj1 = sys.exc_info()
    print obj1[2].tb_lineno
    print dir(obj1)
The traceback object's tb_next attribute and/or the traceback module might be helpful.
Use the traceback module?

Edit: Ninja'ed....
thx it seems working. more question may follow