A program as a system of modules
This system is a Java program: each module is one instruction of main. The try wraps what might fail. The catch waits to one side.
This system is a Java program: each module is one instruction of main.
The try wraps what might fail. The catch waits to one side.
This system is a Java program: each module is one instruction of main. The try wraps what might fail. The catch waits to one side.
The light blue particle is the execution flow: it advances in order.
It prints "Start", divides 10 by 2 and shows the result: 5.
With nothing exceptional, the flow is normal and the catch is not used.
The light blue particle is the execution flow: it advances in order. It prints "Start", divides 10 by 2 and shows the result: 5. With nothing exceptional, the flow is normal and the catch is not used.
Now the operation tries to divide by zero. It cannot be completed.
Java creates an object: an instance of ArithmeticException.
The normal flow is interrupted: the rest of the try does not run.
Now the operation tries to divide by zero. It cannot be completed. Java creates an object: an instance of ArithmeticException. The normal flow is interrupted: the rest of the try does not run.
Now the operation lives in another method: main() calls process()…
…and process() calls calculate(). The call stack records that path.
Now the operation lives in another method: main() calls process()… …and process() calls calculate(). The call stack records that path.
The exception is raised in calculate(). No catch: Java abandons that method.
It moves up to process(), which does not handle it either: abandoned too.
That is how it propagates through the call stack until it reaches main().
The exception is raised in calculate(). No catch: Java abandons that method. It moves up to process(), which does not handle it either: abandoned too. That is how it propagates through the call stack until it reaches main().
In main(), the call was inside a try with a compatible catch.
The catch recognizes the ArithmeticException type and captures the object.
Its instructions run. The program does not go back to the line that failed.
In main(), the call was inside a try with a compatible catch. The catch recognizes the ArithmeticException type and captures the object. Its instructions run. The program does not go back to the line that failed.
When the catch ends, execution goes through the finally block.
It is the completion route: it runs with or without an exception.
When the catch ends, execution goes through the finally block. It is the completion route: it runs with or without an exception.
The exception was handled: the program continues after the try-catch-finally.
An exception changes the normal flow of execution…
…but Java gives us mechanisms to manage it and build more robust programs.
The exception was handled: the program continues after the try-catch-finally. An exception changes the normal flow of execution… …but Java gives us mechanisms to manage it and build more robust programs.