Java Reference
In-Depth Information
The Class Exception
Every exception class is a descendent class of the class Exception . You can use the class
Exception itself in a class or program, but you are even more likely to use it to define a
derived class of the class Exception . The class Exception is in the java.lang package
and so requires no import statement.
Self-Test Exercises
3. What output is produced by the following code?
int waitTime = 46;
try
{
System.out.println("Try block entered.");
if (waitTime > 30)
throw new Exception("Over 30.");
else if (waitTime < 30)
throw new Exception("Under 30.");
else
System.out.println("No exception.");
System.out.println("Leaving try block.");
}
catch (Exception thrownObject)
{
System.out.println(thrownObject.getMessage());
}
System.out.println("After catch block");
4. Suppose that in Self-Test Exercise 3 , the line
int waitTime = 46;
is changed to
int waitTime = 12;
How would this affect the output?
5. In the code given in Self-Test Exercise 3 , what are the throw statements?
6. What happens when a throw statement is executed? This is a general question.
Explain what happens in general, not simply what happens in the code in Exercise 1
or some other sample code.
7. In the code given in Self-Test Exercise 3 , what is the try block?
(continued)
 
Search WWH ::




Custom Search