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--
>>>
沒有留言:
張貼留言