Created
January 10, 2017 15:15
-
-
Save chendi0x7C00/3dc98a466a9e677299d00ba206f497d1 to your computer and use it in GitHub Desktop.
use traceback module to get more stackinfo when Exception occus
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # coding:UTF-8 | |
| import sys | |
| import traceback | |
| def a(): | |
| print("a") | |
| b() | |
| def b(): | |
| print("b") | |
| try: | |
| c() | |
| except Exception as e: | |
| print("="*90) | |
| traceback.print_exc() # 1 | |
| print("="*90) | |
| sys.stderr.write(traceback.format_exc()) # 2 | |
| sys.stderr.flush() | |
| print("="*90) | |
| type, val, tb = sys.exc_info() # 3 | |
| print(type) | |
| print(val) | |
| for filename, linenum, funcname, source in traceback.extract_tb(tb): | |
| print(filename, linenum, funcname, source) | |
| def c(): | |
| print("c") | |
| raise IndexError("some error") | |
| def main(): | |
| a() | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment