例外は、プログラムの実行中に発生する予期しないイベントです。としても知られています ランタイムエラー 。そのエラーが発生すると、 パイソン 実行中に例外が生成され、その例外は処理できるため、プログラムの中断が防止されます。
例: このコードでは、システムは数値をゼロで除算できないため、例外が発生します。
Python3
a>=> 5> b>=> 0> print>(a>/>b)> |
>
>
出力
Traceback (most recent call last): File '/home/8a10be6ca075391a8b174e0987a3e7f5.py', line 3, in print(a/b) ZeroDivisionError: division by zero>
try、excel、else、finally による例外処理
- 試す : このブロックは例外エラーの発生をテストします。
- を除外する : ここでエラーを処理できます
- それ以外 : 例外がない場合、このブロックが実行されます。
- ついに : 最後に、例外が生成されるかどうかにかかわらず、ブロックは常に実行されます。
Python Try、Except、else、Finally の構文
try: # Some Code.... except: # optional block # Handling of exception (if required) else: # execute if no exception finally: # Some code .....(always executed)>
「」の働き 試す' そして 'を除外する'
まずは仕組みを理解しましょう Python の試してみて除外する 作品
- 初め 試す 句が実行されます。つまり、間のコードが実行されます。 試す そして を除外する 句。
- 例外がない場合のみ、 試す 条項が実行されます。 を除外する 句は実行されません。
- 何らかの例外が発生した場合、 試す 条項はスキップされ、 を除外する 条項が実行されます。
- 何らかの例外が発生した場合でも、 を除外する コード内の句では処理されず、外側の句に渡されます。 試す 発言。例外が処理されないままになると、実行が停止します。
- あ 試す ステートメントには複数の文を含めることができます を除外する 句。
例: ユーザーの整数入力を受け取り、例外ブロックで例外をスローしてみましょう。
Python3
コマンドチャウン
# Python code to illustrate working of try()> def> divide(x, y):> >try>:> ># Floor Division : Gives only Fractional> ># Part as Answer> >result>=> x>/>/> y> >print>(>'Yeah ! Your answer is :'>, result)> >except> ZeroDivisionError:> >print>(>'Sorry ! You are dividing by zero '>)> > # Look at parameters and note the working of Program> divide(>3>,>2>)> divide(>3>,>0>)> |
>
>
出力:
Yeah ! Your answer is : 1 Sorry ! You are dividing by zero>
Python で複数の例外をキャッチする
以下は、複数の例外句を使用してさまざまな例外を処理する方法を示す例です。
Python3
try>:> >x>=> int>(>input>(>'Enter a number: '>))> >result>=> 10> /> x> except> ZeroDivisionError:> >print>(>'You cannot divide by zero.'>)> except> ValueError:> >print>(>'Invalid input. Please enter a valid number.'>)> except> Exception as e:> >print>(f>'An error occurred: {e}'>)> |
>
>
出力:
Enter a number: An error occurred: EOF when reading a line>
Python の Else 節
Try 句によって例外が発生しない場合にのみ、コードは else ブロックに入ります。
例: Else ブロックが実行されます 例外が発生していない場合のみ 。
Python3
# Python code to illustrate working of try()> def> divide(x, y):> >try>:> ># Floor Division : Gives only Fractional> ># Part as Answer> >result>=> x>/>/> y> >except> ZeroDivisionError:> >print>(>'Sorry ! You are dividing by zero '>)> >else>:> >print>(>'Yeah ! Your answer is :'>, result)> > # Look at parameters and note the working of Program> divide(>3>,>2>)> divide(>3>,>0>)> |
>
>
出力:
Yeah ! Your answer is : 1 Sorry ! You are dividing by zero>
Python ついにキーワード
Python はキーワードを提供します ついに 、つまり 常に実行される try とブロックを除外した後。 finally ブロックは、try ブロックが正常に終了した後、または try ブロックが何らかの例外により終了した後に常に実行されます。たとえ、Except ブロックで戻ったとしても、Finally ブロックは実行されます。
例: 例外ブロックで例外をスローしてみましょう。最後に、例外が生成されるか生成されないかを実行します。
Python3
# Python code to illustrate> # working of try()> def> divide(x, y):> >try>:> ># Floor Division : Gives only Fractional> ># Part as Answer> >result>=> x>/>/> y> >except> ZeroDivisionError:> >print>(>'Sorry ! You are dividing by zero '>)> >else>:> >print>(>'Yeah ! Your answer is :'>, result)> >finally>:> ># this block is always executed> ># regardless of exception generation.> >print>(>'This is always executed'>)> # Look at parameters and note the working of Program> divide(>3>,>2>)> divide(>3>,>0>)> |
>
>
出力:
Yeah ! Your answer is : 1 This is always executed Sorry ! You are dividing by zero This is always executed>