Code too large for try statement ?
If you didn’t already know it, the java language specification has limits on the size of a method. That limit is 65535 (number of bytes that the code occupies).
This so called limitation has never really bothered me. After practicing object oriented programming, modularization and good design principles you can only ask yourself ‘Who is crazy enough to write a method whose size is greater than 65535 !?’. But you know what ? It can happen. There are so many code gen tools out there and common libraries that will write code for you. Rules engines, JSP compilers, scripting/template engines will write dynamic code for you. If they do not write it withing the 65535 bytes limit, your code will not compile or will give a cryptic error and die.
Code suffering from this phenomenon will see this message – “Code too large to compile” or “Code too large for try statement“. So umm.. where did the try statement come from ?
Jave Server Page (JSP) templates sometimes follow a syntax that resembles this
# Include Header
try
{
# Content
}
catch(Exception e)
{
//Handle it
}
# footer
If you end up writing complex logic in your JSP and include (static includes) other files to partake in the final output, you might end up breaking your page. The error is not necessarily well defined. The same JSP page which might compile with version 8 of weblogic will fail in version 10 or might compile in tomcat and fail in JBOSS. This is because each container writes code within the JSP in its own way.
The try catch may not even be introduced by you. Your container may introduce the try catch (for whatever internal reasons) for code written in the JSP. This problem is famous for creeping up when you move from one container to another or shift between container versions. The error occurs either when you request for a JSP page the first time (and the container tries to compile it) or when you try to pre compile the JSP for performance reasons.
Solution:
Help your container modularize the code. Try splitting the JSP into several chunks and make dynamic includes instead of static ones. Dynamic includes (jsp:include against %@include) will compile the JSP and then stream the output instead of trying to include all the code into one big method or try catch block.
You can read up on the limitation here
http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html
Look for this text
The value of the code_length item must be less than 65536
Normally, with well designed code (front and backend) this wouldn’t happen, but as you say, code generation or other container initiated activity might cause this. Good to point it out though; it is one of those things that will almost never happen, but when it does it would be really hard to find the cause.
article is presented nicely.
problem and solution… good
yes, it’s interesting to know that limit in methods ……..
Good article. Presented well.
A good article!
Thanks
Thanks! Dynamic includes was my solution in my JSP-code
very nice Article
Nice and simple explanation. Its good that you pointed out that each container handles compilation in its own way so something that works fine in one container may blow up in another. One has to look into this on a case by case basis, there is no “one way” to get rid of this problem.