exception

An error object a program raises to stop normal flow and report that something went wrong.

In more detail

When code cannot continue normally it raises or throws an exception, which travels back up through the calling functions until a handler catches it. If nothing catches it, the program usually stops and prints a stack trace. Each kind of error is usually its own class, such as TypeError, so code can catch exactly the ones it expects.

Examples

RecursionError and TypeError appear in this course.
In Python you catch them with try and except; an uncaught one stops a Kafka consumer at the failing record.
JavaScript uses throw and try/catch; the lessons throw descriptive errors for a layer inversion or a compound subcomponent used outside its provider.
Java: every exception extends Throwable and is either checked or unchecked.
throw new IOException("unavailable");
int("abc") raises ValueError: invalid literal for int() with base 10
If an exception thrown during React rendering is not caught by an error boundary, React removes the whole app from the screen. Suspense uses a similar throwing mechanism internally with promises.
In this course: In Python you raise an exception with the raise statement and handle it with try and except. Each kind of error is a class, such as ValueError or KeyError, so code can catch exactly the ones it expects. Tests check that code refuses bad input by asserting that the right exception is raised.

Related terms

Taught in

References

Certification courses