How to retrieve the file, line number, and type of an exception in Python

Retrieving the exception information of a file returns the file in which the exception occurred, the line number of the exception, and the type of exception.

Solution for How to retrieve the file, line number, and type of an exception in Python : You can use sys.exc_info() to retrieve the file, line number, and type of exception In an exception block, call sys.exc_info() to return a tuple containing the exception type, the exception object, and the exception traceback.

Use traceback.exception_file.tb_frame.f_code.co_filename with traceback as the exception traceback from the previous step to return the file name from which the code was compiled. To retrieve the line number in which the exception occurred, call traceback.tb_lineno with traceback as the exception traceback.


how-to-retrieve-the-file-line-number-and-type-of-an-exception-in-python