try構文(エラー処理)
例外処理とかエラートラップなどと呼ばれるやつ。
▼書式
try: この中でエラーが起きたら except: ここの処理を行う
▼使い方
>>> ========================= RESTART ========================= >>> def test1(a, b): c = a + b print c >>> test1(1, "hello") #test1関数のなかで数字と文字列を足すことになる。 Traceback (most recent call last): File "", line 1, in -toplevel- test1(1, "hello") File " ", line 2, in test1 c = a + b TypeError: unsupported operand type(s) for +: 'int' and 'str' >>> def test2(a, b): try: c = a + b print c except TypeError: print "エラーがでたよ" >>> test2(1, "hello") エラーがでたよ 参考:8. エラーと例外 http://www.python.jp/doc/release/tut/node10.html