try: You do your operations here; ...................... except Exception: If there is Exception, then execute this block. ......................
pyuser@deyu:~/zzz$ cat try2.py
#!/usr/bin/python
def div(x, y):
try:
return x/y
except ZeroDivisionError:
print("除數不能為 0")
return
print("6/2=" , div(6,2))
print("3/0=" , div(3,0))
print("10/2=" , div(10,2))
pyuser@deyu:~/zzz$ ./try2.py 6/2= 3.0 除數不能為 0 3/0= None 10/2= 5.0