raise statement
The statement that signals an error by creating and throwing an exception, as in raise ValueError("width must be positive").
In more detail
A function that cannot do its job should raise rather than return a misleading value; the caller decides how to handle it. A bare raise inside an except block re-raises the current exception after you have done some handling. Choose the built-in exception that fits: ValueError for a bad value, TypeError for a wrong type.
Related terms
- exception — An error object a program raises to stop normal flow and report that something went wrong.
- exception chaining — Linking a new exception to the one that caused it, so the higher-level failure still carries the original error and its stack trace.
- custom exception — An exception class you define yourself, usually in two lines by subclassing Exception, to name an error specific to your program.
- try statement — The statement that runs a block of code and, if a listed exception happens there, runs an except block instead of letting the program stop.
Taught in
- Literals, Identifiers And Keywords — Hands-on Python: Complete Video Course & Book
- Input, And Numbers Versus Digits — Hands-on Python: Complete Video Course & Book
- Local Scope And Global Constants — Hands-on Python: Complete Video Course & Book
- Exceptions: try, except, else, finally — Hands-on Python: Complete Video Course & Book
- Raising, And Writing Your Own Exception — Hands-on Python: Complete Video Course & Book
- Testing With pytest — Hands-on Python: Complete Video Course & Book
- The Exception Family Tree — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Print, Input And The Casts Between — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Conditions The Exam Loves — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Control Flow Sprint — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Lists: Slices, Methods And Aliases — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Tuples, And The Comma That Makes Them — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Dictionaries: Keys, Values And Items — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Strings: Escapes, Slices And Methods — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Try, Except, Else And Finally — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Twelve Traps, One Shape Each — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Mock, Review And Exam Day — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- The Class That Shared One List — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- math, random And platform — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Handlers, args, raise And assert — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Exception Classes Of Your Own — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Exceptions Sprint — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Introspection: hasattr, __name__, __module__ And __bases__ — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Generators And Iterators — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Streams, Handles And Open Modes — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- read, readline, errno And bytearray — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Twelve Traps, Mostly Objects — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Mock, Review And Exam Day — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep