Exception Types in Java
An exception is an event that disrupts the normal flow of control in a program.
Try it: try / except / else / finally
How an exception jumps out of a try block to the first matching except clause, when else runs, and why finally always runs.
How it works
- Statements in try run until one raises an exception; the rest of try is skipped.
- Python checks the except clauses in order and runs the first one whose type matches.
- If nothing was raised, the else block runs instead.
- finally runs in every case.
Default run (9 steps): Call safe_divide('4'). … Return 25.0. Output was: Worked: 25.0 / Done.
Simplified: One fixed function; int() and / are modelled exactly (including how Python prints floats), but nothing is executed as Python.
Loading the simulation…
When a Program Leaves Its Planned Path
Most programs are written as a sequence of steps that the computer carries out one after another. Usually that sequence is the path the program follows from start to finish. Sometimes, though, something happens while the program is running that pulls it off that path. Programmers have a word for these moments: exception. This article explains what an exception does to the path a program takes, how Java represents exceptions, and why the word was chosen over the more familiar word error.
Keep one question in mind as you read: when something unusual happens, which statement runs next? The answer to that question is the core of what an exception is.
Tracing the Normal Flow of Control
The phrase normal flow of control refers to the order in which a program's statements are executed when nothing unusual happens. Picture the statements as a list. The computer works down the list, finishing one statement before starting the next. Each statement's position determines when it runs.
- 1Statement 1 runs. Nothing unusual happens, so the program moves on in its planned order.
- 2Statement 2 runs. Nothing unusual happens, so the program moves on again.
- 3During Statement 3, an event occurs that the normal path did not anticipate. The event interrupts the planned sequence.
- 4Because of the interruption, the program does not simply continue to Statement 4. Execution is redirected to a different part of the program, one that has been arranged to deal with the event.
- 5Once that handling is finished, the program is no longer following the original plan from the same point. The path it took was changed by the event.
Read the diagram from top to bottom. The top box is the ordinary path. The event in the middle is the point where that path is interrupted. The exception object is the form the event takes once it exists in the program. The try..catch handler sits at the bottom because exceptions are the kind of event a programmer may want to handle with a try..catch. The diagram maps the idea rather than every step the runtime performs.
The Defining Feature: Disruption
An exception is an event that disrupts the normal flow of control in a program.
The disruption is the defining feature. The event changes which part of the program runs next, instead of letting the statements continue in their planned order. Notice what the definition does not say. It does not say that an exception is always wrong, always caused by a bug, or always fatal. It says only that the event changes the path.
Exceptions as Objects of Type Exception
In Java, exceptions are represented as objects of type Exception. This matters because an object can be created, passed around, and examined like any other value in the program. An exception is not just an abstract idea floating in the background; it is something the program can actually hold and work with.
Exception is the general category. The actual exceptions a program meets are usually defined by subclasses of Exception, which are narrower classes built on top of it. So the general type describes the idea of a disruption, and each subclass describes a more specific kind of disruption.
Subclasses of Exception
Different subclasses represent different types of exceptions. Each one stands for a particular category of disruption, while all of them share the general Exception type. The diagram below shows the relationship the source describes: the general Exception type with specific subclasses beneath it.
The source names two example subclasses and then limits its discussion to them. It notes that many other exception types exist beyond these two. The source also does not describe any further relationship between these two subclasses, so the diagram shows them side by side rather than inventing a parent-child link between them.
Why Exception Is Preferred to Error
It would be natural to call every disruption an error, but the source deliberately avoids that word. The reason is that in some cases an exception might not be considered an error at all. You can sometimes think of an exception as just another way to organize a program. An event that changes the path through the code is not always a mistake. It may simply be a situation the program was designed to route differently.
Tracing a Disruption Step by Step
Following One Event Through the Program
A program is running a list of statements. Statement 3 triggers an event that the program has been designed to route to a handler. Trace what happens to the normal flow of control.
- 1Identify the planned path
Without any event, the program would run Statement 3, then Statement 4, then Statement 5, in that order.
- 2Detect the event
During Statement 3, an event occurs. By the source's definition, this is an exception because it disrupts the normal flow.
- 3Recognize the object form
In Java the event exists as an object of type Exception, or one of its subclasses, which the program can hold and examine.
- 4Redirect execution
Statement 4 does not run next. Execution moves to the handler arranged for this kind of event.
- 5Judge the meaning
Whether this event is a real error or an ordinary routed situation depends on the program's design. The term exception is accurate either way.
The event disrupted the normal sequence at Statement 3, the program's path was rerouted to a handler, and the term exception applies whether or not the event was a mistake.
Mistakes Learners Make with Exception Terminology
Common mistake Assuming every exception is an error
Why it failsThe source states that some exceptions are not errors at all and can be a way to organize a program.
Do this insteadTreat exception as a description of a disruption to flow, and decide separately whether the event is a problem.
Common mistake Thinking an exception is only a message
Why it failsThe source says exceptions are represented as objects of type Exception, which can be created, passed around, and examined.
Do this insteadThink of an exception as an object that carries the event through the program.
Common mistake Treating Exception as one specific kind of exception
Why it failsException is the general category. Specific exception types are usually subclasses of it.
Do this insteadUse Exception for the general idea and name the specific subclass when you mean a particular kind.
Common mistake Inventing a relationship between NumberFormatException and IllegalArgumentException
Why it failsThe source does not describe any further relationship between these two subclasses.
Do this insteadDescribe each one only as a specific subclass of Exception, unless a source explicitly states otherwise.
Practice: Classifying Disruptions
A program runs five statements in order. During statement 2, an event occurs and execution is redirected to a handler. Before reading further, describe in your own words what happened to the normal flow of control, and say whether the word error or the word exception is more accurate for this event if the source's definitions are your guide.
Hints
- Recall the source's definition: an exception is an event that disrupts the normal flow of control.
- The source says exception is preferred to error because some exceptions are not errors at all.
- The question of whether the event is a problem is separate from whether it disrupted the flow.
Name the two example exception subclasses from the source, and state the one relationship the source says is shown in the diagram between each of them and the general Exception type.
Hints
- The source names
NumberFormatExceptionandIllegalArgumentException. - The diagram shows both as specific kinds beneath the general Exception type.
Key Takeaways
- An exception is an event that disrupts the normal flow of control, changing which part of the program runs next instead of letting statements continue in their planned order.
- In Java, exceptions are represented as objects of type Exception, which can be created, passed around, and examined like other values.
- Specific exception types are usually subclasses of Exception; the source names
NumberFormatExceptionandIllegalArgumentExceptionas examples. - The word exception is preferred to error because some exceptions are not errors at all and can simply be a way to organize a program.