Help needed regarding try - catch block
Eshwari PattapathuSubramanianSundaran <[email protected]> Sat, 10 Sep 2005 19:09:45 -0700 (PDT)
| Newsgroups | gmane.comp.jakarta.bcel.user |
|---|---|
| Message-ID | <[email protected]> |
Hi,
I'm trying to encapsulate an instruction in a try block and in the catch block, I'm trying to add a series of instructions as instruction list. Here is the code for the same
try_start = linenogen[lineindex].getInstruction();
GOTO g = new GOTO(null);
InstructionHandle try_end = il.append(g);
InstructionList catchlist = new InstructionList();
InstructionFactory factory = new InstructionFactory(mpool);
catchlist.append(factory.createFieldAccess("java.lang.System", "out", p_stream,
Constants.GETSTATIC));
catchlist.append(new PUSH(mpool, "Exception caught"));
catchlist.append(factory.createInvoke("java.io.PrintStream", "println", Type.VOID,
new Type[] { Type.STRING }, Constants.INVOKEVIRTUAL));
InstructionHandle catch_inst = il.append(catchlist);
methodGen.addExceptionHandler(try_start, try_end, catch_inst, exception);
g.setTarget(ihnext);
methodGen.setMaxStack();
classgen.removeMethod(methods[mindex]);
classgen.addMethod(methodGen.getMethod());
il.dispose();
When I try to run the modified class, I get the following exception.
java.lang.VerifyError: (class: bcelPackage/CorrectiveClass, method: printString signature: ()V) Falling off the end of the code
at InsertTryCatch.main(InsertTryCatch.java:43)
Exception in thread "main"
If the catchlist has only the return instruction, InstructionConstants.RETURN, then the class compiles and runs perfectly without any errors.
Please help me where I'm going wrong.
Thanks,
Eshwari
---------------------------------
Click here to donate to the Hurricane Katrina relief effort.