December 1999
JP219 : JAVA PROGRAMMING

QUESTION 3

Total Marks: 15 Marks

Click here to access other questions

SUGGESTED SOLUTIONS
Solutions and allocated marks are indicated in green.
Return to
Question 3

(a)

(i)State when an exception is thrown in a Java program.[1 mark ]
(i)An exception is thrown when an unexpected error condition is
encountered.

(ii)Briefly state the purpose of an exception handler .[1 mark ]
(ii)The exception handler is responsible for ensuring that the exception is correctly dealt with.[1 mark ]

 

[2]
(b)

(i)Declare an exception class ,called NoSuchAttribute ,which contains the following members:
•A public member AttrName ,of type String ;
•A public member NewValue ,of type Object ;
•A public member Test ,of type boolean ;
•A constructor method,which takes a parameter x ,of type String ,and y ,of type Object .and sets the members AttrName and NewValue equal to x and y respectively. [4 marks ]
(i)A sample definition of NoSuchAttribute follows:

public class NoSuchAttribute extends Exception {

public String AttrName;
public Object NewValue;
public boolean Test;
NoSuchAttribute(String x,Object y){

AttrName=x;
NewValue=y;

}

And the following marking scheme should be used:
•Declaring the class to extend Exception ;(1 mark)
•Declaring the public members AttrName ,NewValue and Test ;(1 mark)
•Declaring the correct constructor signature with parameter list;(1 mark)
•Performing the correct assignments in the constructor method.(1 mark).

(ii)Write a method,called FooBar ,which takes three parameters,x and y ,and Test of types String ,Object ,and boolean respectively.Your method should simply allocate and throw as an exception an object of type NoSuchAttribute ,if the parameter Test is False .[4 marks ]
A sample definition of FooBar follows:

public void FooBar(String x,Object y,boolean Test)

throws NoSuchAttribute {

if(Test==False)

throw new NoSuchAttribute(x,y);

}

And the following marking scheme should be used:
•A correct method signature,with correct use of the throws specifier;(1 mark)
•A correct conditional statement;(1 mark)
•Correctly allocating the NoSuchAttribute object by use of the new operator;
(1 mark)
•Correctly throw ’ing this object.(1 mark)

(iii)Write a method,called TryMyException ,the signature of which is given below, which will try to execute your method FooBar .The method should call the method Failure ,the signature of which is given below,if the exception NoSuchAttribute is caught,and call the method Success ,the signature of which is given below,regardless of which exceptions are caught.You are not required to implement Success or Failure .
void Success();
void Failure();
void TryMyException(String x,Object y,boolean Test); [5 marks ]
(iii)A sample definition of TryMyException follows:

void TryMyException(String x,Object y,boolean Test){

try {

FooBar(x,y,Test);

}catch (NoSuchAttribute e){

Failure();

}finally {

Success();

}

}

And the following marking scheme should be used:
•Using the try statement;(1 mark)
•To guard the call to FooBar();(1 mark)
•Using catch to attempt to call the NoSuchAttribute exception;(1 mark)
•Using finally ;(1 mark)
•Calling Success()and Failure in the correct clauses.(1 mark)

[13]