Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ArrayIndexOutOfBoundsException not being caught and ignored

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

like image 421
Ankur Avatar asked Nov 18 '09 05:11

Ankur


2 Answers

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.

like image 165
digiarnie Avatar answered Sep 28 '22 02:09

digiarnie


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.

like image 23
akf Avatar answered Sep 28 '22 03:09

akf