2018年1月10日 星期三

Python: Print the current call stack

Python: Print the current call stack

Python Basic: Exercise-96 with Solution

Write a Python program to print the current call stack.
Sample Solution:-
import sys
import traceback

def func1():
    raise NameError("--func1 exception--")

def func2():
    func1()

def main():
    try:
        func2()
    except Exception as e:
        traceback.print_exc(limit=1, file=sys.stdout)


if __name__ == '__main__':
    main()

========== RESTART: F:\Python_APSC\py-ex-basic-96.py ==========
Traceback (most recent call last):
  File "F:\Python_APSC\py-ex-basic-96.py", line 13, in main
    func2()
NameError: --func1 exception--
>>> 

沒有留言:

張貼留言

OPEN DATA AQI 空氣品質監測網 + NODE-RED

OPEN DATA AQI 空氣品質監測網 + NODE-RED  首頁 - 空氣品質監測網  https://airtw.moenv.gov.tw/ https://data.gov.tw/datasets/search?p=1&size=10&s=_score...