Programming Basics
What Is Exception Handling?
Exception handling is how a program catches errors that happen while it runs, such as a missing file, and responds without crashing.
Definition
An exception is an error that happens while a program runs, such as dividing by zero, opening a file that is not there or converting "abc" to a number. Exception handling is the code that catches these errors and decides what to do next.
Without it, an uncaught exception stops the program and prints a stack trace.
How it works
Code that might fail goes in a try block. A catch block (except in Python) runs only if a matching error is raised. A finally block runs either way and is used for cleanup, such as closing a file.
You can also raise (throw) your own exceptions to signal that a function received input it cannot handle.