lectures.htm
 

Assignment 6

Class names: MyFirstException, ExceptionThrower, ExceptionRunner
File name: MyFirstException.java, ExceptionThrower.java, ExceptionRunner.java
Type: Application

Due Date: 6-23-03 for full credt
6-26 for a grade starting at 45 points


Assignment Submission Form

Description: This assignment you will construct three classes, MyFirstException, ExceptionThrower, and ExceptionRunner. Below is a description of each of the classes required.

MyFirstException: This class is your own exception class. Thus, its base class is the Exception class. You are required to create two constructors for this class. The first constructor is the default constructor (no parameters) and it must set its base class message to "MyFirstException: created with the default constructor". The second constructor will take one parameter of type String called message which sets the base class message to the value of the parameter.

ExceptionThrower: This class creates two overloaded methods called, "throwFirstException". The first method takes no parameters and must throw a "MyFirstException" object by using the default constructor above. The second method takes one parameter of type String which sets the message of the MyFirstException object by using the single parameter constructor and throws it. Basically, the only function of these two methods is to throw the "MyFirstException" object.

ExceptionRunner: This class is where it all happens. At this point we have a new Exception type, "MyFirstException" and a class the utilizes it by throwing it, "ExceptionThrower". ExceptionRunner will define the main for the application. In the main you need to construct two objects of type "ExceptionThrower" and make calls to each of the two methods, throwFirstException with no parameters and throwFirstException with a String parameter. The second method call needs to use the String literal,"MyFirstException: created with the one parameter constructor". So your method calls will look similar to:

thrower.throwFirstException();
thrower.throwFirstException("MyFirstException: created with the one parameter constructor");

Both calls should be in their own try/catch block. You must also catch the MyFirstException, print out "Try/Catch #: " + (the exception's message). The # represents the number of the Try/Catch block that is being executed, 1 or 2 and (the exception's message is the message that was set by the Exception's constructor. In addition, both catch clauses must output the stack trace. The second try/catch must have a finally clause that outputs "Fun with Try/Catch". Here is the output from my execution:

Try/Catch 1: MyFirstException: created with the default constructor
MyFirstException: MyFirstException: created with the default constructor
          at ExceptionThrower.throwFirstException(ExceptionThrower.java:20)
          at ExceptionRunner.main(ExceptionRunner.java:19)
Try/Catch 2: MyFirstException: created with the one parameter constructor
MyFirstException: MyFirstException: created with the one parameter constructor
          at ExceptionThrower.throwFirstException(ExceptionThrower.java:31)
          at ExceptionRunner.main(ExceptionRunner.java:27)
Fun with Try/Catch!

The javadocs for my code are HERE.

I have also provide my class diagram:


Objectives:
* Understand how to create an "is a" relationship; MyFirstException is a Exception
* Understand how use the exception handling mechanism in Java
* Understand how overload methods and constructors

 

Home | Syllabus | Lectures | Assignments | Schedule | Coding Conventions | Links