SoFunction
Updated on 2024-11-16

Python implementation to catch the file and specific line where the exception occurred

I'm going to cut to the chase, so let's get right to the code!

try:
  print(a)
except Exception as e:
  print(e)
  print(e.__traceback__.tb_frame.f_globals["__file__"])  # The file where the exception occurred
  print(e.__traceback__.tb_lineno)            # Number of lines where the exception occurred

Additional knowledge:The full error log is typed in catch, including the type of Exception and the number of lines reporting the error.

try(){

}catch(Exception e){

}

Type the full error log in the catch, including the Exception type and the number of lines reporting the error.

Method: Add code to the catch block

1)

ByteArrayOutputStream baos = new ByteArrayOutputStream(); 

      (new PrintStream(baos)); 
      String exception = (); 
      ("baos:" + exception); 

2)

("",e);

Above this Python implementation to capture the exception occurred in the file and the specific line is all that I have shared with you, I hope to be able to give you a reference, and I hope you support me more.