August
1999 QUESTION 3 Total Marks: 20 Marks |
Click here to access other
questions
SUGGESTED SOLUTIONS |
(a) | Define what is meant by the term exception with reference to the Java programming language. | [2] |
An exception is
|
||
(b) | Give two examples of abnormal conditions which may disturb the normal flow of program execution. | [2] |
Examples of abnormal
conditions are :
|
||
(c) | State the purpose of an exception class. | [2] |
An exception class
|
||
(d) | State the purpose of an error class. | [2] |
An Error class
|
||
(e) | Consider the following Java class definition : public class OutOfRange extends Exception { public String GetReason() { } (i) Give the definition of a Java class, called MyClass which will contain a constructor function taking two integer parameters a and b, and a String returning function Foo, which is capable of throwing an exception of type OutOfRange. (ii) Using the Java keyword throw, define the member function Foo such that it will throw the exception OutOfRange if and only if param is equal to or greater than limit, with the string message "parameter out of range". (iii) Using the keyword try and catch, implement the constructor function such that it will try to ensure a is b, and will catch the exception, printing out the reason if it is not. |
[4]
[4] |
(i) A sample
definition of MyClass follows: public class MyClass {
|
||
(ii) A sample
definition of Foo follows: public
Foo (int param, int limit) throws OutOfBounds {
|
||
(iii) A sample
definition of MyClass follows: public MyClass (int a, int b) {
|