如何在PYTHON里捕获和抛出异常
1、result = int(input("What is the result when 1 plus 1?"))#这里需要用户输入整数,但是这里却是字符,那么就显示出错。

3、try: result = int(input("What is the result when 1 plus 1?"))except Exception as result: print("This is unknow error %s. " % result)except: print("The value is wrong!")print("The end!")#这个时候就可以排除所有错误了。

5、try: result = int(input("What is the result when 1 plus 1?"))except Exception as result: print("This is unknow error %s. " % result)else: print("No error!")finally: print("The end!")#但是如果有异常else不会运行。但是finally是会运行的。

7、def test1(): return int(input("What is the result when 1 plus 1?"))try: test1()except Exception as result: print("somthing is wrong %s. " % result) #面对这种情况也是可以在最后面加上try和except来捕获异常。

9、def test(): result = input("What is the result w茑霁酌绡hen 1 plus 1?") if len(result) == 1: return result print("there is an error.") error = Exception("输入错误") raise errortry: print(test())except Exception as result: print(result) #设置try和except在下方,然后正常运行一下。
