PYTHON INTERMEDIATEChapter 7 · Errors, Files and Modules
Raising your own errors
When a function receives something it cannot handle, raise an exception instead of quietly returning a wrong answer: raise ValueError("message"). The caller decides what to do with it using try/except. For problems specific to your program, create a custom exception by subclassing Exception.
Worked example
How it reads
raisestops the function immediately and sends the error up to the callerclass LaunchError(Exception)makes a new kind of error you can catch by nameexcept ... as egives you the error, andprint(e)shows its message

Cloud tip: Raise early, catch late: check inputs at the top of a function, and handle errors where you can actually do something about them.


