Nested Exceptions in Java: Definition & Example

Instructor: Benjamin Blanchard

Ben has taught ESL and web programming and has a M.S. in education.

Using nested exceptions in Java, you can have your program track precisely where unexpected behavior occurred and follow it to its source. Learn how to create chains of exceptions from method to method.

Review of Exceptions

In Java, an exception is a type of object that represents unexpected behavior in a program. When a program encounters something outside the intended parameters of operation, it throws an exception. This means that regular execution of the program is halted and a message about what caused the exception is reported. In the following code, for example, the execution is halted when an attempt is made to divide by zero.

Code example that shows an exception

If you ran this code, you'd notice that it does not go on to execute the print statement but outputs the following error message and exits.

Output example for Java exception

There are many types of exceptions, all of which are subclassed from the class Exception or one of its subclasses. This one is called, appropriately, ''ArithmeticException: / by zero.''

Try and Catch

Sometimes there will be an unavoidable possibility that your code will throw an exception - for instance when a network connection is not available, or some user input does not adhere to certain criteria. When this happens, you have the options of ''catching'' the exception and executing some additional code before the program continues, saving it from having to exit. To do this, you enclose the code in question in a block, preceded by the keyword try. After the try block, you need a catch block, i.e. a block of code that will execute if the exception is caught. Catch takes an argument, like any method invocation; this argument is the exception you're expecting.

Java code example showing try and catch blocks

Running the above code would result in both print lines being executed in the order they appear in the program. Note that the exception is being sent in as a parameter to 'catch' with the name 'e', but it is not used.

Throwing Exceptions

Before we get to the concept of chaining, which is just another term for nested exceptions, we need to go over throwing. In the example above, the exception we caught was thrown by code we didn't see, but only invoked: the built-in division operator ('/'). You can, however, throw exceptions from your own code using, not surprisingly, the keyword 'throw.' In the example given below, you can see this in action.

Example code showing throw in Java

Here, if the test determines that the file does not in fact exist, a new FileNotFoundException is created, and 'throw' returns control to the invoking method. So the line that would try to delete the file never gets a chance to execute, which is good because it would fail.

Throwing Exceptions up the Chain

An exception is said to be 'chained' or 'nested' when it is thrown from a catch block and includes another exception (usually the one that was caught by 'catch') as an argument to its constructor. The exception provided in the new exception's constructor is called the 'cause', because it's likely to be the reason the catch block was executed. Another example will make this clear.

Example java code showing a nested exception

There are a few things to clarify about the above example.

First, the method, deleteFile that throws an exception to the main method has a throws statement as part of its declaration. In many cases, if a method might possibly throw an exception, that must be made explicit using the 'throws' keyword, followed by the type of exception. In this case, we are saying that it throws an Exception, without getting any more specific. This is fine and won't be likely to cause any problems during compile time.

To unlock this lesson you must be a 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

Become a member and start learning now.
Become a Member  Back
What teachers are saying about
Try it now

Earning College Credit

Did you know… We have over 220 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? 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
Used by over 30 million students worldwide
Create an account