Lesson 11
Error Handling
Last updated
Error Handling
Last updated
Author: Brandon Fajardo (11/10/17). Updated (8/7/20)
So far, we have assumed that the user is someone who either 1.) understands how to not break the code or 2.) does not intend to nefariously break the code.
Yet, when we wish to give our code to other people, this is often not the case. For this reason, we often want to prevent certain code-breaking errors from happening. One way we do this is by using try catch.
Try Catch: a set of statements which attempt to prevent exceptions (errors) from occurring.
Try-catch is a two-part statement that initially tries a block of code and catch, in the case of some error, prevents the code from erroring.
The notation is as follows:
This code is a little different from what we usually do in Java. What this does is it creates an error with a customizable message.
Normally, when code is invalid, Java sends out its own error message and stops the code from running. Now, you can send an error which has a custom message. This error is defined in the throw statement.
Throw does not need to be paired with try catch. It can be used otherwise/alone.
EX:
(Example given by Anthony Nuon-Prak)
To be able to catch certain errors, certain imports may be necessary.
Example:
This code attempts to divide 1 by an inputted variable. The first catch statement protects against dividing by zero. The second catch statement protects against using different data types.
Example
Here, even if the catch statement throws a null, the finally statement will still print the numbers from 0 to 100.
If a null is thrown in the finally statement though, everything after the throw will not work though as everything in the statement is still affected by it.
Note:
e is an arbitrary variable and stands for exception. Like other variables, it cannot be used twice without one overwriting the other.
Example - Finally
(Example provided by Letsy Cahue-Flores)
Recap: In this lesson, we learned how to handle errors caused by unexpected actions. We learned about try, catch, and finally. We also learned how to throw errors with throw.
Next: In the next lesson, we will learn about our last topic, arrays.
Statement
Usage
Try
Within this block of code is everything that we wish to check for errors.
You may only use one.
Catch
This statement deals with any errors which may occur in the try block.
You may use any number of catch statements to catch various errors and do different things as a result.
If successful, an error will be handled and the code will not stop running.
Finally
This statement will run its block of code after try and catch regardless of the results of the previous.
Even if an error has occurred that was not handled by a catch, finally will still run.
Error name
Description
ArithmeticException
Math errors (division by 0, even roots of negative numbers)
IOException
Deals with files
Input/Output Exception
InputMismatchException
Deals with Scanner, inputting wrong data type
NullPointerException
Deals with variables (null variables)
IllegalStateException
Deals with “throw clause”
ArrayIndexOutOfBoundsException
Deals with arrays and elements being referenced (by their index) which do not exist