ArrayIndexOutOfBoundsException not being caught and ignored

北战南征 提交于 2019-12-04 10:00:29

Stupid question, but is the ArrayIndexOutOfBoundsException that you put in the catch from the same package as the one being thrown? i.e. java.lang

Or perhaps catch throwable to see if that even works.

It is possible that the code that you are calling is handling the ArrayIndexOutOfBoundsException and printing the the stack trace on its own without rethrowing it. If that is the case, you would not see your System.out.println called.

EDIT: If you want to keep chugging along, it would be good to know that the PDFContentStreamProcessor#processContent will catch the ArrayIndexOutOfBoundsException and then throw an instance of its com.lowagie.text.ExceptionConverter, which is a subclass of RuntimeException.

Maybe this is a no-brainer (after all, I'm running on 3 hours of sleep in the last 36 hours), but along the lines of what digiarnie and Ankur mentioned: have you tried simply catch (Exception e)?

It's definitely not ideal, since obviously it (along with the Throwable t suggestion) will catch every exception under the sun, not limited to ArrayOutOfBoundsException. Just thought idea out there if you haven't tried it yet.

Instead of using this exception, you should fix your code so that you do not go past array boundaries!

Most arrays count from 0 up to array.length-1

If you replace your for loop with this, you might this avoids the entire issue:

for (int page = 0;page < noPages;page++){

you need the try/catch to be inside the forloop. control pops out to the try catch, the catch fires, and resumes control afterwards, but the forloop has already been terminated.

    for(int page=1;page<=noPages;page++)
    {
        try
        {
            content = extractor.getTextFromPage(page); 
            System.out.println(content);
        }
        catch (ArrayIndexOutOfBoundsException e)
        {
            System.out.println("This page can't be read");
        }
    }

Perhaps this is a silly question... Are you sure that the exception is thrown in the code you posted and not in a differen method?

The program should have worked. You should give more details including your class name. You can try by catching Exception or putting a finally block with some s.o.p in it.

This is strange - I actually had a look at itext's source in the method the exception is thrown from (CMapAwareDocumentFont.decodeSingleCID) and it looks like this:

 private String decodeSingleCID(byte[] bytes, int offset, int len){
        if (toUnicodeCmap != null){
            if (offset + len > bytes.length)
                throw new ArrayIndexOutOfBoundsException("Invalid index: " + offset + len);
            return toUnicodeCmap.lookup(bytes, offset, len);
        }

        if (len == 1){
            return new String(cidbyte2uni, 0xff & bytes[offset], 1);
        }

        throw new Error("Multi-byte glyphs not implemented yet");
    }

The ArrayIndexOutOfBoundsException it throws is the standard Java one. I can't see any reason your original try-catch not working.

Perhaps you should post the entire class? Also, which version of itext are you using?

Wait a second! You're missing some braces in there :) Your catch statement is outside your for statement! You have this:

for(int page=1;page<=noPages;page++){
    try{
        System.out.println(page);               
        content = extractor.getTextFromPage(page);
        }
    }   
    catch (ArrayIndexOutOfBoundsException e){
    System.out.println("This page  can't be read");
    }    
}

It should be:

for(int page=1;page<=noPages;page++) {
    try{
        System.out.println(page);               
        content = extractor.getTextFromPage(page);
    }
    catch (ArrayIndexOutOfBoundsException e){
        System.out.println("This page  can't be read");
    } 
} //end for loop  

}//This closes your method or whatever is enclosing the for loop
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!