約 26,000 件の結果
リンクを新しいタブで開く
    1. Use the **raise** keyword to trigger an exception manually.

    2. Specify the exception type you want to raise, such as ValueError, TypeError, or any other built-in or custom exception.

    3. Optionally, include a custom error message by passing it as an argument to the exception type.

    Example Syntax:

    • raise ExceptionType("Custom error message")

    Additional Notes:

    • To re-raise an exception inside an except block, use raise without arguments.

    • For custom exceptions, define a new class inheriting from the Exception base class.

    フィードバック
    ありがとうございました!詳細をお聞かせください
  1. Manually raising (throwing) an exception in Python

    When inside an except clause, you might want to, for example, log that a specific type of error happened, and then re-raise. The best way to do this while preserving the stack tr…
    Don't Raise Generic Exceptions

    Avoid raising a generic Exception. To catch it, you'll have to catch all other more specific exceptions that subclass it.

    Best Practices: Raise Statement

    Instead, use the most specific Exception constructor that semantically fits your issue. which also handily allows an arbitrary number of arguments to be passed to the constructor: These arguments are accessed by the args attrib…

    Create Your Own Error Types When Apropos

    You can create your own error types, if you want to indicate something specific is wrong with your application, just subclass the appropriate point in the exception hierarchy: and usage:

    コード サンプル

    def demo_no_catch():
      try:
        raise Exception('general exceptions not caught by specific handling')
      except ValueError as e:
        print('we will not catch exception: Exception')...
    stackoverflow についてさらに表示
    フィードバック
    ありがとうございました!詳細をお聞かせください
  2. 【Python】例外をスロー(raise)してエラーを管理する方法を初心者 …

    2025年7月20日 · Pythonでの例外スローやエラー処理の方法を、初心者の方にもわかりやすく具体例を交えて解説します。 実務での活用場面を意識したコード例とともに、try-exceptやカスタム例外の …

  3. Pythonの例外処理(try, except, else, finally) | note.nkmk.me

      • 例外処理の基本: try, except。
      • 複数の例外をキャッチ。
      • すべての例外をキャッチ。
      • 正常終了時の処理: else。
      • 終了時に常に行う処理: finally。
  4. Python Try Except - W3Schools

    The try block lets you test a block of code for errors. The except block lets you handle the error. The else block lets you execute code when there is no error. The finally block lets you execute code, regardless …

  5. Python における例外処理 まとめ #error - Qiita

    2024年12月25日 · はじめに 本記事では Python の例外処理についてまとめています。 例外処理は、プログラムの実行中に予期せぬエラーが発生した場合に、プログラムを正常に終了させたり、適切な …

  6. Python 例外処理のサンプル (try…exceptとraise) - ITSakura

    2019年6月26日 · tryの処理で例外が発生した場合、exceptの例外処理が実行されます。 例外クラス名は、キャッチする例外を記述します。 exceptの最後にあるas 変数名は省略可能です。 elseは例外が …

  7. 【try-except文を完全攻略】Pythonの例外(Exception) …

    2025年1月7日 · この記事では、 try-except文を使った例外処理方法を、初心者にもわかりやすく解説します。 この記事を読み進めれば、例外(Exception)の処 …

  8. 【Python】例外処理の使い分けについて:raise文、ass…

    2024年9月26日 · はじめに Pythonにおける例外処理は、プログラムの実行中に発生するエラーを適切に捕捉し、処理を継続したり、エラーの原因を特定したりす …

  9. Built-in Exceptions — Python 3.14.2 documentation

    1 日前 · Built-in Exceptions ¶ In Python, all exceptions must be instances of a class that derives from BaseException. In a try statement with an except clause that …

  10. Python Exception Handling - GeeksforGeeks

    2025年10月11日 · We raise an exception in Python using the raise keyword followed by an instance of the exception class that we want to trigger. We can choose from built-in exceptions or define our own …