2017-02-15 64 views
1

下面的代碼返回:Python:爲什麼這不會拋出異常?

TypeError: cannot concatenate 'str' and 'int' objects

爲什麼它不拋出異常?

while True: 
    try: 
     print "test" + 1 
    except ValueError: 
     print "You can't concatenate that different object types silly" 
+4

'ValueError'和'TypeError'不是同一個詞 – Ryan

+0

這是對你有好處,它沒有運行,這是一個無限循環,如果用TypeError替換ValueError! – MYGz

+1

將'catch ValueError:'更改爲'catch TypeError:'。顯然,除去'while True',除非你想看到你的錯誤消息重複無窮無盡。 – Kevin

回答

2

你能趕上這樣的例外:

try: 
    print "test" + 1 
except ValueError: 
    print "You can't concatenate that different object types silly" 
except TypeError: 
    print "TypeError and the words you want to say" 
相關問題