Copyright

Catching Multiple Exceptions in Java

Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

Computer programs only do what humans tell them to do. If humans make mistakes, so will programs. In Java, an error is an exception. It's handy to know how to handle multiple errors at once:. That's what we'll learn in this lesson.

Multiple Catch Statements

In Java programming, there will be many times when two or more exceptions can occur. What if the user can enter bogus data AND you wind up dividing by zero? Or what if an index that is out of bounds is referenced, a value gets divided by zero AND the file goes missing from the network?

As with single error and exception checking, it's good practice to account for any possible exceptions that could arise. Therefore, we will need a way to catch multiple exceptions and prevent our programs from crashing or giving compiler errors.

Let's look at some examples.

The following code can have a few things go wrong. First, it declares an array, but references an item in the array that doesn't exist (out of bounds). Next, it does some math, so there is the potential that the formula will try to divide by zero.

Therefore, it's good to write a few statements to try to catch the errors. Java provides the try and catch block to complete this. The code statements within the try block is what you want to attempt. Everything inside catch statement(s) will be run if any of the exceptions occur.

The catch statement is followed by parenthesis and the name of the exception. After the name of the exception, we provide a short alias for this exception. Because we are working with classes and object in Java, we can use that alias later if we need to.


Java multiple catch


Notice that there is only one alias for all exceptions: Java is smart enough to catch all of them into one name.

Throwable: There's a Class For That

There are a lot of possible exceptions/errors in programming. In order to keep them all in a single place, and to maintain its object-oriented nature, Java has a built-in class called Throwable.

Here is a small sample of the subclasses within the Throwable class. You'll notice we used some of these in our code.


Java Throwable Class


The Throwable class is included in all Java programs, so there's no need to import any utility.

When we catch multiple exceptions, we need to understand the hierarchy of classes within the Throwable class. For example, input/output exceptions, such as end of file errors, are included in the major input/output exception class. That means that the class EOFException (end of file exception) is a child class of IOException (input/output exception). Therefore, the following code is repetitive:


Java multiple exceptions redundancy


But how would you possibly know which subclasses are of which class?

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support