1. Try and Catch
Now, let's explore Java's exceptional exception handling - one of its many strengths!
2. Problems in Java
Problems are a part of life - and programming! In Java, they are called exceptions. They occur when something goes wrong, like bad user input or a coding mistake, disrupting the program's normal flow. When this happens, we say an exception is thrown.
3. Exception Example
Let's start by seeing an exception in action. If we forget that an `ArrayList` index starts at 0, we might try to access the last object using `.get()` as we see in this code. This leads to an `IndexOutOfBoundsException`, stopping the program before reaching the `println` statement. It also shows the output we see here. Java has many types of exceptions, which we'll explore in the next video. Finally, while it's best to avoid exceptions, Java also provides ways to handle them effectively.
4. Try
To watch for and address exceptions, we add a `try` block to wrap code that might cause an exception. This tells Java to attempt to run this code, and if an issue occurs, it will be sent to a catch block, which we'll explore next.
5. Catch
We use the `catch` keyword with a code block to define what happens when an exception occurs. With `catch`, we can specify the type of exception to handle, or we can use `Exception` to catch any exception type. A variable stores the caught exception, and the block contains statements to run if the exception happens.
6. Catching IndexOutOfBoundsException
Returning to our example, we can catch the `IndexOutOfBoundsException` and print a specific message when it occurs. Instead of handling all exceptions, we're specifically watching for this exception type, ensuring targeted handling.
7. Multiple Catch
Here, we call `Integer.valueOf()` to extract an integer from a `String`, but since the `String` isn't a valid number, a `NumberFormatException` is thrown. We use separate catch blocks to handle different exceptions, allowing Java to check them in order until it finds a match. We can include multiple catch blocks to handle different errors effectively.
8. Catch "All"
We use a catch block with `Exception` as a catch-all for any other exception.
9. Finally
A `finally` block can be added to any `try/catch` block. It runs no matter what - whether an exception was caught and handled or not. This makes it useful for cleanup tasks that should happen after both normal and exception-handling flows. Typical clean up tasks include closing a database or file we have opened to get data from it.
10. Exception object
The `Exception` object passed into each `catch` block contains useful information. Inside the catch block, we can use the `.getMessage()` method to retrieve a message string containing exception details, `.getClass()` method to get the class or type of exception, and the `.printStackTrace()` method to print a stack trace, which helps identify what went wrong and where in the code the exception occurred.
11. Exception stack trace
A stack trace is a complete list of method calls Java executed to get to the failure, as we can see here. In this stack trace, we can see that the main method called the valueOf method which called the parseInt method which is where the Exception occurred.
12. Let's practice!
Ready to work with try and catch?