How do I get the text of a Python exception?

Python Methods and Functions

If Python code throws an exception, we can catch it and print the type, error message, traceback and get information such as the filename and line number in the Python script where the exception occurred.

We can find the type, value, parameters of error tracing

The type gives the type of the exception that occurred; the value contains an error message; traceback contains a snapshot of the stack and much more about the error message.

The sys.exc_info () function returns a tuple of these three attributes, and the Promote operator has a three-argument form that takes these three parts.

Getting the exception type, file number and line number in p code reamer

 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 

Get Solution for free from DataCamp guru