Java Reference
In-Depth Information
4 44 97 Classjava/io/IOException
capture the control for the next two catch-clauses in the source code. The next five entries
in the table
4 44 118 any
55 65 118 any
76 86 118 any
97 107 118 any
118 120 118 any
apply to the try block, the three catch-clauses, and the finally-clause itself. These insure
that if an exception is raised in any of these, the code for the finally-clause is executed:
118:astore 5
120:aload_1
121:invokevirtual #8;//Methodjava/io/FileReader.close:()V
124:aload_2
125:invokevirtual #9;//Methodjava/io/FileWriter.close:()V
128:aload 5
130:athrow
Notice copies of the finally-clause code follow the try block and each of the catch-clauses,
insuring that the finally code is always executed 10 .
Invoke CLEmitter 's addExceptionHandler() method for emitting each entry in the
table. This method uses labels for identifying locations and is discussed in Appendix D
(D.3.2).
Exercise 5.17. Add the throws-clause to j--, adding it to your compiler and testing it
thoroughly. Make sure all the rules of exception handling are covered.
Much of the work here is ensuring that any exception that might be possibly thrown in
the invocation of the method is either caught by a catch-clause in a surrounding try-catch
block, or is covered by one of the exceptions in the throws declaration. See Section 11.2 of
the Java Language Specification [Gosling et al., 2005].
As far as the generated code goes, one needs to ensure that the invocation of the method
for adding a new method,
publicvoidaddMethod(ArrayList<String>accessFlags,
Stringname,
Stringdescriptor,
ArrayList<String>exceptions,
booleanisSynthetic)
includes a list of the exceptions that the method throws, in internal form.
Exercise 5.18. Modify the Copy class to catch possible exceptions in the finally-clause:
finally{//closethefiles
try{
if(inStream!=null){
inStream.close();
}
}
catch(Exceptione){
System.err.println("Unabletocloseinputstream.");
}
try{
10 Another way to handle the nally-clause is to treat it as a subroutine, making use of the JVM's jsr
and ret instructions. Consult Chapter 7 of [Lindholm and Yellin, 1999] for hints on using these instructions
for exception handling.
 
Search WWH ::




Custom Search