Java Reference
In-Depth Information
LISTING 11.4
continued
//-----------------------------------------------------------------
// Performs a calculation to produce an exception. It is not
// caught and handled at this level.
//-----------------------------------------------------------------
public void level3 ()
{
int numerator = 10, denominator = 0;
System.out.println("Level 3 beginning.");
int result = numerator / denominator;
System.out.println("Level 3 ending.");
}
}
A programmer must pick the most appropriate level at which to
catch and handle an exception. There is no single best answer as to
how to do this. It depends on the situation and the design of the sys-
tem. Sometimes the right approach will be not to catch an exception
at all and let the program terminate.
KEY CONCEPT
A programmer must carefully con-
sider how and where exceptions
should be handled, if at all.
SELF-REVIEW QUESTIONS (see answers in Appendix N)
SR 11.7 What happens if an exception is not caught?
SR 11.8 How would the result of the Propagation program change if the fol-
lowing code fragment was placed in the level2 method just before the
call to the level3 method?
int num = 10, den = 0;
int res = num / den;
SR 11.9 How would the result of the Propagation program change if the fol-
lowing code fragment was placed in the level2 method just after the
call to the level3 method?
int num = 10, den = 0;
int res = num / den;
 
Search WWH ::




Custom Search