📄 chap18.html
字号:
<P> static int hopAround() {</P>
<P> int i = 0;</P>
<P> while (true) {</P>
<P> try {</P>
<P> try {</P>
<P> i = 1;</P>
<P> }</P>
<P> finally { // The first finally clause</P>
<P> i = 2;</P>
<P> }</P>
<P> i = 3;</P>
<P> // This return never completes, because of</P>
<P> // the continue in the second finally clause</P>
<P> return i;</P>
<P> }</P>
<P> finally { // The second finally clause</P>
<P> if (i == 3) {</P>
<P> // This continue overrides the return statement</P>
<P> continue;</P>
<P> }</P>
<P> }</P>
<P> }</P>
<P> }</P>
<P>}</P>
</FONT><FONT SIZE="2"><P> </P></FONT><FONT FACE="Courier New">end</FONT></P></PRE>
<P>The bytecodes generated by <CODE>javac</CODE> for the <CODE>hopAround()</CODE> method are shown below:</P>
<PRE><P><FONT FACE="Courier New">begin</FONT></P>
<FONT SIZE="2"><P></FONT><FONT FACE="Courier New"> 0 iconst_0 // Push constant 0
<P> 1 istore_0 // Pop into local var 0: int i = 0;</P>
<P> </P>
<P>// Both try blocks start here (see exception table, below):</P>
<P> 2 iconst_1 // Push constant 1</P>
<P> 3 istore_0 // Pop into local var 0: i = 1;</P>
<P> 4 jsr 18 // Jump to mini-subroutine at offset 18 (the</P>
<P> // first finally clause)</P>
<P> 7 goto 24 // Jump to offset 24 (to just below first</P>
<P> // finally clause)</P>
<P> </P>
<P>// Catch clause for the first finally clause:</P>
<P>10 astore 4 // Pop the reference to thrown exception, store</P>
<P> // in local variable 4</P>
<P>12 jsr 18 // Jump to mini-subroutine at offset 18 (the</P>
<P> // first finally clause)</P>
<P>15 aload 4 // Push the reference (to thrown exception)</P>
<P> // from local variable 4</P>
<P>17 athrow // Rethrow the same exception</P>
<P> </P>
<P>// The first finally clause:</P>
<P>18 astore 5 // Store the return address in local variable 5</P>
<P>20 iconst_2 // Push constant 2</P>
<P>21 istore_0 // Pop into local var 0: i = 2;</P>
<P>22 ret 5 // Jump to return address stored in local variable 5</P>
<P> </P>
<P>// Bytecodes for the code just after the first finally clause:</P>
<P>24 iconst_3 // Push constant 3</P>
<P>25 istore_0 // Pop into local var 0: int i = 3;</P>
<P> </P>
<P>// Bytecodes for the return statment:</P>
<P>26 iload_0 // Push the int from local</P>
<P> // variable 0 (i, which is 3)</P>
<P>27 istore_1 // Pop and store the int into local</P>
<P> // variable 1 (the return value, i)</P>
<P>28 jsr 39 // Jump to mini-subroutine at offset 39 (the</P>
<P> // second finally clause)</P>
<P>31 iload_1 // Push the int from local variable 1 (the</P>
<P> // return value)</P>
<P>32 ireturn // Return the int on the top of the stack</P>
<P> </P>
<P>// Catch clause for the second finally clause:</P>
<P>33 astore_2 // Pop the reference to thrown exception, store</P>
<P> // in local variable 2</P>
<P>34 jsr 39 // Jump to mini-subroutine at offset 39 (the</P>
<P> // second finally clause)</P>
<P>37 aload_2 // Push the reference (to thrown exception)</P>
<P> // from local variable 2</P>
<P>38 athrow // Rethrow the same exception</P>
<P> </P>
<P>// The second finally clause:</P>
<P>39 astore_3 // Store the return address in local variable 3</P>
<P>40 iload_0 // Push the int from local variable 0 (i)</P>
<P>41 iconst_3 // Push constant 3</P>
<P>42 if_icmpeq 47 // If the top two ints on the stack are equal, jump</P>
<P> // to offset 47: if (i == 3) {</P>
<P>45 ret 3 // Jump to return address stored in local variable 3</P>
<P>47 goto 2 // Jump to offset 2 (the top of the while</P>
<P> // block): continue;</P>
<P> </P>
<P>Exception table:</P>
<P> from to target type</P>
<P> 2 4 10 any</P>
<P> 2 31 31 any</P>
</FONT><FONT SIZE="2"><P> </P></FONT><FONT FACE="Courier New">end</FONT></P></PRE>
<P>The <CODE>hopAround()</CODE> method returns from the first <CODE>finally</CODE> clause by executing past the closing curly brace, but returns from the second <CODE>finally</CODE> clause by executing a <CODE>continue</CODE> statement. The first <CODE>finally</CODE> clause, therefore, exits via its <EM><FONT FACE="Courier New">ret</FONT></EM> instruction. But because the second <CODE>finally</CODE> clause exits via a <CODE>continue</CODE>, its <EM><FONT FACE="Courier New">ret</FONT></EM> instruction is never executed. The <CODE>continue</CODE> statement causes the Java Virtual Machine to jump to the top of the <CODE>while</CODE> loop again. This results in an endless loop, even though it is a <CODE>return</CODE> statement that originally causes the second <CODE>finally</CODE> clause to be executed in the first place. The continue statement in the <CODE>finally</CODE> clause supersedes the <CODE>return</CODE> statement, so the method never returns. </P>
<P>Note that the bytecodes that implement the <CODE>return</CODE> statement store a copy of the return value into local variable one before jumping to the miniature subroutine that represents the second <CODE>finally</CODE> clause. Then, after the miniature subroutine returns (in this case it never does, because the continue is always executed), the return value is retrieved from local variable one and returned.</P>
<P>This highlights the way the Java Virtual Machine returns values when <CODE>finally</CODE> clauses are also executed. Rather than returning the value of <CODE>i</CODE> after the <CODE>finally</CODE> clause is executed, the Java Virtual Machine will return the value that <CODE>i</CODE> had just <I>before</I> the <CODE>finally</CODE> clause was executed. This means that even if the <CODE>finally</CODE> clause changes the value of <CODE>i</CODE>, the method will still return the value that <CODE>i</CODE> had when the <CODE>return</CODE> statement was first reached, before the <CODE>finally</CODE> clause was invoked. If you wanted the <CODE>finally</CODE> clause to be able to change the return value of the method, you would have to put an actual <CODE>return</CODE> statement with the new return value into the <CODE>finally</CODE> clause itself. </P>
<P>To drive the <I>Hop Around</I> simulation, use the Step, Reset, Run, and Stop buttons. Each time you press the Step button, the simulator will execute the instruction pointed to by the pc register. If you press the Run button, the simulation will continue with no further coaxing on your part until you press the Stop button. To start the simulation over, press the Reset button. For each step of the simulation, a panel at the bottom of the applet contains an explanation of what the next instruction will do. Happy clicking.</P>
<P><IMG SRC="fig18-1.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/venners/images/fig18-1.gif" ALT="Figure 18-1"></P>
<H3><EM><P>On the CD-ROM</P>
</EM></H3><P> The CD-ROM contains the source code examples from this chapter in the <FONT FACE="Courier New">opcodes</FONT> directory. The <I>Hop Around</I> applet is contained in a web page on the CD-ROM in file <FONT FACE="Courier New">applets/HopAround.html</FONT>. The source code for this applet is found alongside its class files, in the <FONT FACE="Courier New">applets/JVMSimulators</FONT> and <FONT FACE="Courier New">applets/JVMSimulators/COM/artima/jvmsim</FONT> directories.</P>
<H3><EM><P>The Resources Page</P>
</EM></H3><P>For more information about the material presented in this chapter, visit the resources page: <FONT FACE="Courier New"><A HREF="http://www.artima.com/insidejvm/finally.html" tppabs="http://www.artima.com/insidejvm/finally.html">http://www.artima.com/insidejvm/finally.html</A></FONT>.</P>
<TABLE BORDER="0" WIDTH="100%">
<TR><TD><A HREF="http://www.pbg.mcgraw-hill.com/betabooks/stores.html" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/stores.html" target="bottom"><IMG SRC="hotkey.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/images/hotkey.gif" ALIGN="LEFT" BORDER="0" WIDTH="40" HEIGHT="40" ALT="Orders"></A>
<IMG SRC="order_text.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/images/order_text.gif" WIDTH="103" HEIGHT="41" ALT="Orders"></TD>
<TD ALIGN="RIGHT"><A HREF="chap17.html" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/venners/chap17.html"><IMG SRC="backward.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/images/backward.gif" BORDER="0" ALT="Backward" WIDTH="32" HEIGHT="32"></A> <A HREF="chap19.html" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/venners/chap19.html"><IMG SRC="forward.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/images/forward.gif" BORDER="0" ALT="Forward" WIDTH="32" HEIGHT="32"></A></TD></TR>
<TR><TD COLSPAN="2"><A HREF="mailto:computing@mcgraw-hill.com"><IMG SRC="hotkey.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/images/hotkey.gif" ALIGN="LEFT" BORDER="0" WIDTH="40" HEIGHT="40" ALT="Comments"></A>
<IMG SRC="comment_text.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/images/comment_text.gif" WIDTH="73" HEIGHT="39" ALT="Comments"></TD></TR>
</TABLE>
<HR>
<P ALIGN=CENTER> <A HREF="http://www.pbg.mcgraw-hill.com/computing/computing-home.html" tppabs="http://www.pbg.mcgraw-hill.com/computing/computing-home.html" TARGET="_top">COMPUTING
MCGRAW-HILL</A> | <A HREF="http://www.pbg.mcgraw-hill.com/betabooks/betabooks-home.html" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/betabooks-home.html" TARGET="_top">Beta Books</A>
| <A HREF="http://www.pbg.mcgraw-hill.com/computing/contact.html" tppabs="http://www.pbg.mcgraw-hill.com/computing/contact.html" TARGET="_top">Contact Us</A>
| <A HREF="http://www.pbg.mcgraw-hill.com/betabooks/stores.html" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/stores.html" TARGET="_top">Order Information</A>
| <A HREF="http://mcgraw-hill.inforonics.com/compsearch.shtml" tppabs="http://mcgraw-hill.inforonics.com/compsearch.shtml" TARGET="_top">Online Catalog</A></P>
<P ALIGN=CENTER><FONT SIZE="-1"><A HREF="http://www.pbg.mcgraw-hill.com/computing/computing-home.html" tppabs="http://www.pbg.mcgraw-hill.com/computing/computing-home.html" TARGET="_top">Computing McGraw-Hill</A> is an imprint of the <A HREF="http://www.pbg.mcgraw-hill.com/pbg-home.html" tppabs="http://www.pbg.mcgraw-hill.com/pbg-home.html" TARGET="_top">McGraw-Hill Professional Book Group</A>.</FONT></P>
<!-- begin footer -->
<HR>
<A HREF="http://www.mcgraw-hill.com/" tppabs="http://www.mcgraw-hill.com/" TARGET="_top"><IMG SRC="division-white.gif" tppabs="http://www.pbg.mcgraw-hill.com/images/division-white.gif" WIDTH="350" HEIGHT="44" ALT="A Division of the McGraw-Hill Companies" BORDER="0"></A><BR>
<FONT SIZE="-2">Copyright © 1997 <A HREF="http://www.mcgraw-hill.com/" tppabs="http://www.mcgraw-hill.com/" TARGET="_top">
The McGraw-Hill Companies</A>. All rights reserved. Any use is subject to the
<A HREF="http://www.mcgraw-hill.com/corporate/news_info/copyrttm.htm" tppabs="http://www.mcgraw-hill.com/corporate/news_info/copyrttm.htm" TARGET="_top">
Terms of Use</A>; the corporation also has a comprehensive <A HREF="http://www.mcgraw-hill.com/corporate/news_info/privacy.html" tppabs="http://www.mcgraw-hill.com/corporate/news_info/privacy.html" TARGET="_top">
Privacy Policy</A> governing information we may collect from our customers.</FONT>
<!-- end footer -->
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -