i

JAVA Complete Course

Nested Try Statements

We have already seen an example of multiple catch statements but we also can have in our application multiple try statements and even more, they can be nested in each other. Here we need to remember that each try statement must have its catch clause. Let’s see an example of nested try statements:

A screenshot of a cell phone

Description automatically generated

In this example we first try to access random index of array and in first try we have a case of catching ArrayIndexOutOfBoundException and then in nested try we may expect division by zero, which will give us ArithmeticException.