ArrayIndexOutOfBoundsException not being caught and ignored

▼魔方 西西 提交于 2019-12-21 16:21:00

问题


I want to catch and ignore and ArrayIndexOutOfBoundsException error (basically it's not something I have control over, so I need my program to keep chugging along).

However my try/catch pair doesn't seem to catch the exception and ignore it. Hopefully you can pick out what I am doing wrong.

The exception occurs at this line

content = extractor.getTextFromPage(page);

Here is my code:

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");
    }    
}

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Invalid index: 02 at com.lowagie.text.pdf.CMapAwareDocumentFont.decodeSingleCID(Unknown Source) at com.lowagie.text.pdf.CMapAwareDocumentFont.decode(Unknown Source) at com.lowagie.text.pdf.parser.PdfContentStreamProcessor.decode(Unknown Source) at com.lowagie.text.pdf.parser.PdfContentStreamProcessor.displayPdfString(Unknown Source) at com.lowagie.text.pdf.parser.PdfContentStreamProcessor$ShowText.invoke(Unknown Source) at com.lowagie.text.pdf.parser.PdfContentStreamProcessor.invokeOperator(Unknown Source) at com.lowagie.text.pdf.parser.PdfContentStreamProcessor.processContent(Unknown Source) at com.lowagie.text.pdf.parser.PdfTextExtractor.getTextFromPage(Unknown Source) at com.pdfextractor.main.Extractor.main(Extractor.java:64)

edit: I have put the try/catch within the for loop
and added the stack trace
and removed index=1


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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++){



回答5:


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.




回答6:


    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");
        }
    }



回答7:


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?




回答8:


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.




回答9:


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?




回答10:


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


来源:https://stackoverflow.com/questions/1753752/arrayindexoutofboundsexception-not-being-caught-and-ignored

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