Catching Exceptions


This Magercise shows how to catch two common exceptions: NullPointerException and ArrayIndexOutOfBoundsException.

Exceptions are covered in detail in the course notes.

Work Location

Perform all work for this magercise in VisualAge project MageLang Magercises, package magercises.catchclause.

If this project does not appear in your Workspace add it from the repository (if it exists there) or create a new project using this name.

Tasks

Perform the following tasks:

  1. Add a try and catch block to the main you have been provided. Check for NullPointerException and ArrayIndexOutOfBoundsException exceptions coming from the calls to search. The two last search calls generate the exceptions--you will have to comment out the second search to get the third to be executed. Generate "array was null" upon NullPointerException and "n>array.length" upon ArrayIndexOutOfBoundsException.

The task numbers above are linked to the step-by-step help page. Also available is a complete solution to the problem, and expected behavior, to demonstrate it.

Copyright © 1996-1997 MageLang Institute. All Rights Reserved.