Java Reference
In-Depth Information
Self-Test Exercises
1. 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");
2. Suppose that in exercise 1, the line
int
waitTime = 46;
is changed to
int
waitTime = 12;
How would this affect the output?
3. In the code given in exercise 1, what are the
throw
statements?
4. What happens when a
throw
statement is executed? This is a general question. Tell
what happens in general, not simply what happens in the code in exercise 1 or some
other sample code.
5. In the code given in exercise 1, what is the
try
block?
6. In the code given in exercise 1, what is the
catch
block?
7. In the code given in exercise 1, what is the
catch
block parameter?
8. Is the following legal?
Exception exceptionObject =
new
Exception("Oops!");
9. Is the following legal?
Exception exceptionObject =
new
Exception("Oops!");
throw
exceptionObject;