ArrayIndexOutOfBoundsException will not be caught and ignored

I want to catch and ignore the ArrayIndexOutOfBoundsException error (basically this is not what I have control, so I need my program to continue moving).

However, my try / catch pair does not seem to throw an exception and ignore it. I hope you can choose what I am doing wrong.

An exception occurs on 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");
    }    
}

< > "main" java.lang.ArrayIndexOutOfBoundsException: : 02   at com.lowagie.text.pdf.CMapAwareDocumentFont.decodeSingleCID( )   at com.lowagie.text.pdf.CMapAwareDocumentFont.decode( )   at com.lowagie.text.pdf.parser.PdfContentStreamProcessor.decode( )   at com.lowagie.text.pdf.parser.PdfContentStreamProcessor.displayPdfString( )   at com.lowagie.text.pdf.parser.PdfContentStreamProcessor $ShowText.invoke( )   at com.lowagie.text.pdf.parser.PdfContentStreamProcessor.invokeOperator( )   at com.lowagie.text.pdf.parser.PdfContentStreamProcessor.processContent( )   at com.lowagie.text.pdf.parser.PdfTextExtractor.getTextFromPage( )    com.pdfextractor.main.Extractor.main(Extractor.java:64)

edit: try/catch for index = 1

+5
10

, ArrayIndexOutOfBoundsException, catch , , ? java.lang

, , , , .

+3

, , , ArrayIndexOutOfBoundsException , . , System.out.println.

: , , PDFContentStreamProcessor#processContent ArrayIndexOutOfBoundsException com.lowagie.text.ExceptionConverter, RuntimeException.

+4

, ( , 3 36 ), , digiarnie Ankur: catch (Exception e)?

, , ( Throwable t) , ArrayOutOfBoundsException. , , .

+3

, !

0 array.length-1

for , :

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

, try/catch forloop. try, , forloop .

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

, ... , , , differen?

0

. , . , Exception finally s.o.p .

0

- itext , (CMapAwareDocumentFont.decodeSingleCID), :

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

An exception to ArrayIndexOutOfBoundsException is standard Java. I see no reason why your original try-catch does not work.

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

0
source

Wait a second! You are missing some braces there :) Your expression about leaving outside of your statement! You have the following:

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
0
source

All Articles