Store exception body in a variable

Is there a way to execute the try and return the body of the error as a variable?

i.e.

 var = '' try: error generating code except: var = exception_body 
+6
source share
1 answer

Yes, use as except syntax:

 try: raise Exception("hello world") except Exception as x: print(x) 

In earlier versions of Python, this would be written except Exception, x: which you may see from time to time.

+11
source

Source: https://habr.com/ru/post/906229/


All Articles