简体   繁体   中英

Continue script execution after try/except exception with Python

Coming from other scripting languages I'm probably missing something fundamental here.

How do I continue on with the rest of the script when an exception is raised?

Example:

errMsg = None
try:
    x = 1
    y = x.non_existent_method()
except ValueError as e:
    errMsg = str(e)

if errMsg:
    print('Error')
else:
    print('All good')

The script just fails. Is there a way to continue with the rest?

Thanks in advance

Sorry for the stupid question. It should be 'Exception' and not 'ValueError'

errMsg = None
try:
    x = 1
    y = x.non_existent_method()
except Exception as e:
    errMsg = str(e)

if errMsg:
    print('Error')
else:
    print('All good')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM