Home > java > Code too large for try statement ?

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
# 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.


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


Look for this text

The value of the code_length item must be less than 65536

Categories: java Tags: , ,
  1. August 20th, 2009 at 13:24 | #1

    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.

  2. August 21st, 2009 at 11:06 | #2

    article is presented nicely.
    problem and solution… good :)

  3. Srikanth
    August 22nd, 2009 at 17:37 | #3

    yes, it’s interesting to know that limit in methods ……..

  4. Indu
    December 9th, 2010 at 07:42 | #4

    Good article. Presented well.

  5. February 6th, 2011 at 10:39 | #5

    A good article!

    Thanks :)

  6. Ville
    September 29th, 2011 at 08:09 | #6

    Thanks! Dynamic includes was my solution in my JSP-code :)

  7. Rajendra Verma
    December 9th, 2011 at 15:03 | #7

    very nice Article

  8. Max
    May 17th, 2012 at 17:00 | #8

    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.

  1. No trackbacks yet.