Catch any error in Python

前端 未结 8 1664
一个人的身影
一个人的身影 2021-02-01 14:24

Is it possible to catch any error in Python? I don\'t care what the specific exceptions will be, because all of them will have the same fallback.

8条回答
  •  借酒劲吻你
    2021-02-01 15:13

    Not mentioning the type of exception you want to handle itself does the job.

    try this:

        try:
           #code in which you expect an exception 
        except:
           #prints the exception occured
    

    if you want to know the type of exception occurred:

        try:
           #code in which you expect an exception 
        except Exception as e:
           print(e)
           #for any exception to be catched
    

    for detailed explanation go trough this https://www.tutorialspoint.com/python/python_exceptions.htm

提交回复
热议问题