All You Ever Wanted to Know About Java Exceptions

The Why, How, Dos and Don’ts

Ben Weidig
The Startup
Published in
15 min readMay 17, 2020

--

Photo by John Matychuk on Unsplash

Exception handling is a mechanism used to handle disruptive, abnormal conditions to the control flow of our programs. The concept can be traced back to the origins of Lisp and is used in many different programming languages today.

If a program reaches such an abnormal condition, the Java runtime tries to find an appropriate exception handler by going through the call stack. This behavior is called “exception propagation”, and behaves differently regarding the type of exception.

Different Kinds of Exceptions

Exceptions can be categorized into 2 categories that differ in coding requirements and behavior: Checked and Unchecked Exceptions.

Checked Exceptions

Checked exceptions are anticipated, and recoverable events outside of the normal control flow.

A good example is FileNotFoundException or MalformedURLException. Because they are supposed to be anticipated, the compiler makes sure we declare them on our method signatures, and enforces either handling or propagating them further up the call stack. This is called the "catch or specify requirement".

--

--

Ben Weidig
The Startup

Software developer, entrepreneur, blogger. Mostly Java, sometimes Swift, Golang, Bash and all the other fun stuff.