Exceptions&Files

Exceptions

You have already seen exceptions in previous code.They occur when something goes wrong,due to incorrect code or input.When an exception occurs,the program immediately stops.

The following code produces the ZeroDivisionError exception by trying to divide 7 by o.

1 #!/usr/bin/env python
2 # -*- coding:utf-8 -*-
3 num1=7
4 num2=0
5 print(num1/num2)

result:

1 Traceback (most recent call last):
2   File "./1.py", line 5, in <module>
3     print(num1/num2)
4 ZeroDivisionError: integer division or modulo by zero
原文地址:https://www.cnblogs.com/scholarly/p/10272130.html