December 1999
JP219 : JAVA PROGRAMMING

QUESTION 3

Total Marks: 15 Marks

Click here to access other questions

Click to access
SUGGESTED SOLUTIONS
for Question 3

(a)

(i)State when an exception is thrown in a Java program.[1 mark ]

(ii)Briefly state the purpose of an exception handler .[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 ]

(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 ]

(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 ]

[13]