Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Click Here!
Click Here!
ITKnowledge
Find:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


Exceptions thrown by a method

The .class file also tells you which checked exceptions a method can throw. A checked exception is one that you must catch or declare in a throws clause. The exceptions declared in the throws clause of a method are an attribute of the method. The ExceptionsAttribute class, Listing 4-34, holds an array of indices into the constant pool, each of which points to a ClassInfo structure. The ClassInfo structure represents the class of the exception that’s thrown.

Listing 4-34 The ExceptionsAttribute class

   import java.io.*;

   public class ExceptionsAttribute {

    int nameIndex;
    int[] exceptions;

    public ExceptionsAttribute(AttributeInfo ai) throws IOException {
     nameIndex = ai.nameIndex();
     ByteArrayInputStream bis = new ByteArrayInputStream(ai.data);
     DataInputStream dis = new DataInputStream(bis);
     exceptions = new int[dis.readUnsignedShort()];
     for (int i = 0; i < exceptions.length; i++) {
      exceptions[i] = dis.readUnsignedShort();
     }
    }

    public int nameIndex() {
     return nameIndex;
    }

    public int howMany() {
     return exceptions.length;
    }

    public int getIndex(int i) {
     return exceptions[i];
    }

    public String toString() {
     return String.valueOf(nameIndex);
    }

   }


Secret:  In the Disassembler class, the getExceptions() method returns a throws clause for a particular method. The exceptions, if any, are stored in an attribute of the method with the name “Exceptions.” This attribute does not necessarily exist. Methods that declare no exceptions will not have an Exceptions attribute.


Secret:  This differs from what appears in the Java Virtual Machine Specification. According to that document, “There must be exactly one Exceptions attribute in each method info structure.” However, current Java compilers do not write an exceptions attribute in the method _ info structure unless the method actually has a throws clause. Listing 4-35 demonstrates the getExceptions() method.

Listing 4-35 The getExceptions() method

   public String getExceptions(Method mi) {

    ExceptionsAttribute theExceptions=null;
    String result = “”;

    // find the exceptions attribute
    AttributeInfo[] mAttributes = mi.getAttributes();
    for (int i = 0; i < mAttributes.length; i++) {
     String name = thePool.readUTF8(mAttributes[i].nameIndex());
     if (name.equals(“Exceptions”)) {
      try {
       theExceptions = new ExceptionsAttribute(mAttributes[i]);
      }
      catch (IOException e) {
      }
      break;
     }
    }
    if (theExceptions != null) {
     for (int i = 0; i < theExceptions.howMany(); i++) {
      if (i == 0) result += “ throws “;
      else result += “, “;
      ClassInfo ci = thePool.readClassInfo(theExceptions.getIndex(i));
      result += thePool.readUTF8(ci.nameIndex()).replace(‘/’, ‘.’);
     }
    }

    return result;

   }

The method body

The one piece left is the code inside the methods. This is the one piece of a Java .class file that you can’t easily make to match the source code. That’s because the Java source language in which you write programs is compiled to the much lower level byte code.

In this chapter, I only show you where the bytes of the byte code are stored so that you can output them in a disassembly. The next chapter, however, discusses what those byte codes mean, how you can read and understand them, and how you can work backward from the byte codes to Java source code.

The byte codes for each method are stored in a Code attribute for the method. The Code attribute has many different fields, but most of them are used only when interpreting code. In this chapter, you see only the actual byte codes.

The constructor has more information to parse than you need immediately. The toString() method converts the signed bytes in the code array to integers between zero and 255. Listing 4-36 shows this CodeaAttribute class.


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.