So far we've used only single-level try/catch/finally statements, but exception-handling logic can also be nested. A try/catch/finally statement can appear inside the try, catch, or finally block of another try/catch/finally statement. This hierarchical nesting allows any block in a try/catch/finally statement to execute code that might, itself, throw an error.
For example, suppose we were writing a multiuser, web-based message board system. We define the following classes: BulletinBoard, the application's main class; GUIManager, which manages the user interface; and User, which represents a user on the board. We give BulletinBoard a method, populateUserList( ), which displays the list of current active users. The populateUserList( ) method splits its work into two stages: first it retrieves an instance of a List class from the application's GUIManager instance. The List class represents an onscreen user list. Then populateUserList( ) populates that List instance with users from a supplied array of User instances. These two stages can both potentially generate an exception, so a nested try/catch/finally structure is used in the populateUserList( ) method. Let's take a closer look at this nested structure.
During the first stage of populateUserList( ), if the List instance isn't available, a UserListNotFound exception is thrown by the GUIManager. The UserListNotFound exception is caught by the outer try/catch/finally statement.
If, on the other hand, the List instance ...