主页 Swift UNIX C Assembly Go Plan 9 Web MCU Research Non-Tech

try...except...finally statement in Python

2022-06-03 | Web | #Words: 119

try...except...finallystatement is used to handle errors and return information. The format is as follows:

try:
	code
except:
	code
finally:
	code

try indicates attempting to execute this code. If unsuccessful, the except section will be returned. However, regardless of the attempted execution result, the content of finally will be returned.

For example:

#!/usr/bin/python3

try:
        x>3
except:
        print('error')
finally:
        print('finally')

Return:

$ ./try.py 
error
finally