try-except-elseが存在する理由は何ですか?
tryブロックは、あなたが予想されるエラーを処理することができます。exceptブロックはあなたが処理するために用意されている例外をキャッチする必要があります。予期しないエラーを処理すると、コードが誤った動作を行い、バグを隠す可能性があります。
elseエラーがなかった場合句が実行され、そしてその中でコードを実行していないことにより、tryブロック、予期しないエラーをキャッチ避けます。繰り返しになりますが、予期しないエラーをキャッチするとバグを隠すことができます。
例
例えば:
try:
    try_this(whatever)
except SomeException as the_exception:
    handle(the_exception)
else:
    return something
「try、except」スイートには、2つのオプションの節がelseありfinallyます。ですから、それは実際try-except-else-finallyです。
elsetryブロックからの例外がない場合にのみ評価されます。これにより、以下のより複雑なコードを簡略化できます。
no_error = None
try:
    try_this(whatever)
    no_error = True
except SomeException as the_exception:
    handle(the_exception)
if no_error:
    return something
したがって、else(バグを作成する可能性がある)代替案と比較すると、コード行が減り、コードベースが読みやすく、保守可能で、バグが少ないことがわかります。
finally
finally returnステートメントで別の行が評価されている場合でも、何が実行されます。
疑似コードで分解
コメントを付けて、すべての機能を示す可能な限り小さい形式で、これを分解することが役立つ場合があります。これが構文的に正しい(ただし、名前が定義されていない限り実行できない)疑似コードが関数内にあると想定します。
例えば:
try:
    try_this(whatever)
except SomeException as the_exception:
    handle_SomeException(the_exception)
    # Handle a instance of SomeException or a subclass of it.
except Exception as the_exception:
    generic_handle(the_exception)
    # Handle any other exception that inherits from Exception
    # - doesn't include GeneratorExit, KeyboardInterrupt, SystemExit
    # Avoid bare `except:`
else: # there was no exception whatsoever
    return something()
    # if no exception, the "something()" gets evaluated,
    # but the return will not be executed due to the return in the
    # finally block below.
finally:
    # this block will execute no matter what, even if no exception,
    # after "something" is eval'd but before that value is returned
    # but even if there is an exception.
    # a return here will hijack the return functionality. e.g.:
    return True # hijacks the return in the else clause above
代わりに、ブロック内のコードをブロック内のブロックに含めることができます。例外がなければ実行されますが、そのコード自体がキャッチしている種類の例外を発生させたらどうなるでしょうか。それをブロックに残すことは、そのバグを隠すでしょう。elsetrytry
tryブロック内のコード行を最小限に抑えて、コードが失敗した場合に大音量で失敗させたいという原則のもと、予期しない例外をキャッチしないようにしています。これがベストプラクティスです。
  例外はエラーではないと私は理解しています
Pythonでは、ほとんどの例外はエラーです。
pydocを使用して、例外の階層を表示できます。たとえば、Python 2の場合:
$ python -m pydoc exceptions
またはPython 3:
$ python -m pydoc builtins
階層を提供します。ほとんどの種類のExceptionエラーはエラーであることがわかりますが、Pythonはforループの終了(StopIteration)などにエラーの一部を使用します。これはPython 3の階層です:
BaseException
    Exception
        ArithmeticError
            FloatingPointError
            OverflowError
            ZeroDivisionError
        AssertionError
        AttributeError
        BufferError
        EOFError
        ImportError
            ModuleNotFoundError
        LookupError
            IndexError
            KeyError
        MemoryError
        NameError
            UnboundLocalError
        OSError
            BlockingIOError
            ChildProcessError
            ConnectionError
                BrokenPipeError
                ConnectionAbortedError
                ConnectionRefusedError
                ConnectionResetError
            FileExistsError
            FileNotFoundError
            InterruptedError
            IsADirectoryError
            NotADirectoryError
            PermissionError
            ProcessLookupError
            TimeoutError
        ReferenceError
        RuntimeError
            NotImplementedError
            RecursionError
        StopAsyncIteration
        StopIteration
        SyntaxError
            IndentationError
                TabError
        SystemError
        TypeError
        ValueError
            UnicodeError
                UnicodeDecodeError
                UnicodeEncodeError
                UnicodeTranslateError
        Warning
            BytesWarning
            DeprecationWarning
            FutureWarning
            ImportWarning
            PendingDeprecationWarning
            ResourceWarning
            RuntimeWarning
            SyntaxWarning
            UnicodeWarning
            UserWarning
    GeneratorExit
    KeyboardInterrupt
    SystemExit
コメント者は尋ねました:
  外部APIにpingするメソッドがあり、APIラッパーの外側のクラスで例外を処理したい場合、except句の下のメソッドからeを返すだけですか?eは例外オブジェクトですか?
いいえ、例外は返されませんraise。スタックトレースを保持するために、ベアでそれを再レイズするだけです。
try:
    try_this(whatever)
except SomeException as the_exception:
    handle(the_exception)
    raise
または、Python 3では、新しい例外を発生させ、例外チェーンを使用してバックトレースを保持できます。
try:
    try_this(whatever)
except SomeException as the_exception:
    handle(the_exception)
    raise DifferentException from the_exception
ここで私の答えを詳しく説明します。