Exception & Error
Key points:
- Error is not equal to Exception
- Exception 异常 是在出现错误时采用正常控制流以外的动作
How to handle Exception:
- Detected error, thus triggering exception 检测到错误,引发异常
- Catch the exception 对异常进行捕获的操作
Syntax:
1
2
3
4
5
6
try:
detecting exception
except Exception[, reason]:
catch exception code
finally:
implement code no matter if got exception
Types of Exception
SyntaxError
语法错误
NameError
variable命名错误
IndexError
string index out of range
1
2
a = '123'
print(a[3])
KeyError
IndexError: string index out of range
1
2
3
d = {'a':1, 'b':2}
print(d['c'])"
ValueError
ValueError - wrong value
1
2
3
4
5
6
7
try:
year = int(input(""please input here:""))
except ValueError:
print('You need to input numbers!')
>>>please input here:""asdf"
>>>You need to input numbers!
AttributeError
AttributeError: ‘int’ object has no attribute ‘append’
1
2
a = 123
a.append()
ZeroDivisionError
1
2
3
4
5
try:
print(1/0)
except ZeroDivisionError:
print('you cant use 0 to divide 1')
How to catch exception
1.Print error message when catching exception
捕获错误时也打印出错误信息
Example 1: You can’t use 0 to divide 1 integer division or modulo by zero
1
2
3
4
try:
print(1/0)
except ZeroDivisionError as e:
print('you cant use 0 to divide 1 %s' %e)
Example 2: Please input here:”asdf”
1
2
3
4
5
try:
year = int(input(""please input here:""))
except ValueError as e:
print('You need to input numbers! %s' %e)
>>>You need to input numbers! invalid literal for int() with base 10: 'asdf'
2.Catch ALL exception and print error message
1
2
3
4
5
6
7
try:
year = int(input(""please input here:""))
except Exception as e:
print('You need to input numbers! %s' %e)
>>>please input here:"asdf"
>>>You need to input numbers! invalid literal for int() with base 10: 'asdf'
3.Create your own exception message
1
2
3
4
5
6
try:
raise NameError('helloError')
except NameError:
print('my custom error')
>>>my custom error
finally
finally
- 无论产生异常与否 都会执行
1
2
3
4
5
6
7
8
try:
a = open('name1.txt')
except Exception as e:
print(e)
finally:
a.close()
>>>NameError: name 'a' is not defined