This video will introduce exceptions and how they are handled in Java.
The following code may have been altered to comply with youtube's video description policy. To download the actual files, go here:
• Java Exception Handling 00. Setting up and...
01. The Call Stack
----------------
What is an exception?
It's a shorthand for an "exceptional event".
In short, it's an event disrupts the normal flow of the execution of a program.
When this abnormal event occurs, the method creates an exception object and hands it off to the runtime system.
This is known as throwing an exception.
How are exception objects thrown and handled?
When an exception object is created and thrown, the runtime system has to take this and find something to handle it (handler).
It looks in the call stack for such a handler. This handler is declared by a "catch" keyword.
main
\/
methodA - if not found, look here!
\/
methodB - Looks for a handler here!
\/
methodC - Error occurred here
// methodC throws an exception
The exception gets thrown in methodC, and we look for the handler in methodB.
If not found, we pass it to methodA, then main.
This sequence of going back to check for handlers is known as the call stack.
| | | | | | | methodC |
| | | | | methodB | | methodB |
| | | methodA | | methodA | | methodA |
| main | | main | | main | | main |
--------- ----------- ---------- ----------
Call Stack
Pops the methods in order, and finds which one has a handler for the thrown exception object.