Python入門的36個例子 之 34 -> Raise An Error – 自定義異常信息

源代碼下載:下載地址在這裏

 

raise有兩個參數,第一個是由我們自己定義的異常類型,第二個是關於此異常的少量說明信息。

 

 

output:

以下是兩次運行的結構

 

>>>
Input your age:22
22
>>>
Input your age:911
Traceback (most recent call last):
  File "C:/Documents and Settings/ning/桌面/Python Intro/038_RaiseAnError.py", line 11, in <module>
    print getAge()
  File "C:/Documents and Settings/ning/桌面/Python Intro/038_RaiseAnError.py", line 6, in getAge
    raise 'BadAgeError', 'It is impossible!!!!!'
TypeError: exceptions must be classes or instances, not str
>>>

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章