Results 1 to 3 of 3
  1. #1
    fedo is offline Member
    Join Date
    Apr 2014
    Rep Power

    Default Manually add a exception table entry

    I use Javassist to manually add a exception table entry:
    Java Code:
    MethodInfo minfo = (MethodInfo) aclasscf.getMethods().get(0);
    CodeAttribute ca = minfo.getCodeAttribute();
    ExceptionTable et = ca.getExceptionTable();
    et.add(26, 30, 40, 0);
    System.out.println("exception table size: " + et.size());
    If I get the bytecode with javap it seems to work:
    Java Code:
    Exception Table
    from to target type
    26   30 40     any
    But the problem is that I can't run the file anymore. Simply running it with the java command results in "java.lang.VerifyError: Expecting a stackmap frame at branch target 40". After some research this seems to be a problem with java 7 and a stricter verifier. I read several times that java -XX:-UseSplitVerifier should be used instead.
    This really fixed the stackmap error, but then another error appeared: "java.lang.VerifyError: (class: AClass, method: signature: ()V) Inconsistent stack height 2 != 1". Are there any further steps required to insert a new exception in the exception table?
    Additional info: Later I want to remove gotos (in this case the goto at line 27) and add a function call instead. When this function finishes it will throw a exception and propagate it back to the caller (which will be at line 27). Then the program should go on as if nothing happend, that is also the reason why the target of the exception is the same as the target of the goto (40)

    Here is the bytecode:
    Java Code:
           0: aload_0       
           1: invokespecial #8                  // Method java/lang/Object."<init>":()V
           4: iconst_0      
           5: istore_1      
           6: goto          19
           9: getstatic     #10                 // Field java/lang/System.out:Ljava/io/PrintStream;
          12: iload_1       
          13: invokevirtual #16                 // Method java/io/PrintStream.println:(I)V
          16: iinc          1, 1
          19: iload_1       
          20: bipush        20
          22: if_icmplt     9
          25: iconst_1      
          26: istore_1      
          27: goto          40
          30: getstatic     #10                 // Field java/lang/System.out:Ljava/io/PrintStream;
          33: iload_1       
          34: invokevirtual #16                 // Method java/io/PrintStream.println:(I)V
          37: iinc          1, 1
          40: iload_1       
          41: bipush        20
          43: if_icmplt     30
          46: return        
        Exception table:
           from    to  target type
              26    30    40   any

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Northern Virginia, United States
    Rep Power

    Default Re: Manually add a exception table entry

    Are you doing this to accomplish something that Java cannot (or can) do or are you just playing around with byte codes?

    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    fedo is offline Member
    Join Date
    Apr 2014
    Rep Power

    Default Re: Manually add a exception table entry

    I am trying to accomplish something that Java (hopefully) can. :)
    It also seems like some people were already able to implement it, as it is described in this paper:
    in the section "Extension to Java Bytecode".

Similar Threads

  1. SQL Exception when creating table
    By wdh321 in forum JDBC
    Replies: 15
    Last Post: 10-23-2012, 06:31 PM
  2. Error: java.lang.Exception: Failed to get cmi entry
    By Faisal Saddique in forum New To Java
    Replies: 7
    Last Post: 02-14-2012, 05:31 PM
  3. Getting exception when updating the table.
    By Chinnu55 in forum Advanced Java
    Replies: 1
    Last Post: 07-14-2011, 06:00 PM
  4. Entry in table
    By Harsh_Verma in forum New To Java
    Replies: 2
    Last Post: 07-01-2009, 02:07 PM
  5. Handle exception manually
    By John_28 in forum New To Java
    Replies: 2
    Last Post: 06-05-2008, 11:26 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts