天天看點

java中的異常--Exceptions in Java-- 第四部分

This coffee has an unusual taste. 

In the <code>UnusualTasteException</code> case, both <code>drinkCoffee()</code> and <code>serveCoffee()</code> methods completed abruptly. The Java virtual machine popped two frames from the Java stack, stopping its popping only when it reached the <code>main()</code> method.

The last case in this example occurs if the variable <code>i</code> in the <code>drinkCoffee()</code> method gets set to a value greater than 2. In this case, the switch statement will instantiate and throw a <code>TemperatureException</code>. When this exception is thrown, the Java virtual machine will go through its usual procedure of examining methods for <code>catch</code> clauses and popping frames for methods that can't handle the exception. The virtual machine will examine <code>drinkCoffee()</code>, pop its frame, examine <code>serveCustomer()</code>, pop its frame, examine <code>main()</code>, and pop its frame. At this point, however, the virtual machine has run out of frames. It can't go any further up the method invocation stack because <code>main()</code> was the first method invoked by the thread.