Python exception

import sys, os

try:
    raise NotImplementedError("No error")
except Exception as e:
    exc_type, exc_obj, exc_tb = sys.exc_info()
    fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
    print(exc_type, fname, exc_tb.tb_lineno)
    import inspect

    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    ENDC = '\033[0m'
    colo = OKGREEN + __file__+"#"+  inspect.stack()[0][3]  + ENDC

    print(colo)

class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'

import inspect

def PrintFrame():
  callerframerecord = inspect.stack()[1]    # 0 represents this line
                                            # 1 represents line at caller
  frame = callerframerecord[0]
  info = inspect.getframeinfo(frame)
  print info.filename                       # __FILE__     -> Test.py
  print info.function                       # __FUNCTION__ -> Main
  print info.lineno                         # __LINE__     -> 13

def Main():
  PrintFrame()                              # for this line

Main()


参考:

https://stackoverflow.com/a/287944/6132577

你可能感兴趣的:(Python exception)