Creating and Throwing Exceptions


This Magercise demonstrates throwing and catching of Exceptions.

Here is the scenario:

You are a quality control engineer at an airplane manufacturer. You are building a computer simulation of airplane construction. In order to best study error handling at the factory, you are using Java exceptions to track errors in the factory.

The given starter program is a skeleton of factory processes. The task is to annotate the code to throw exceptions when one of the process areas is faulty. A faulty area is indicated through a variable set when the factory is constructed (such as enginesGood).

Catch all exceptions as soon as possible. Whenever an exception is caught, print out an appropriate error message. Also throw a ComponentException if one of the component build processes (for engine and frame) is faulty. This exception should be caught by the buildAirplane method.

The defining of exceptions is covered in the course notes.

Work Location

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

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. Fill out the method buildEngine to throw the proper exception if the engine build is bad.

  2. Repeat the above for the methods buildFrame and assembleComponents.

  3. Fill out the method buildComponents. Each exception that can be thrown by the methods calls in buildComponents should be caught individually, an error message printed to System.out, and a new ComponentException thrown.

  4. Fill out the build Airplane method. It should catch the appropriate exceptions and print errors to System.out.

  5. The main method creates some new factories and excercises them. Compile and run the application. Examine the output for correct behavior.

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.