我想捕获并忽略 ArrayIndexOutOfBoundsException 错误(基本上这不是我可以控制的,所以我需要我的程序继续运行)。

但是我的 try/catch 对似乎没有捕获异常并忽略它。希望你能指出我做错了什么。

异常发生在这一行

内容 = extractor.getTextFromPage(page);

这是我的代码:

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在com.lowagie.text.pdf.cmapawaredocumentfont.decodesinglecid(unknown source)(com.lowagie.text.pdf.cmapawaredocumentfont.decode(unewnown) ) 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. pdfContentsStreamProcessor.invokeoperator(未知来源),请访问com.lowagie.text.pdf.parser.pdfcontentstreamprocessor.processcontent(com.lowagie.lowagie.text.pparser.pparser.pdftextextextexttract.tructtract.pettrextfrompfrompfrompfrompfrompfrompfrompfrompfrompfrompfrompfrompfrompfrompfrompfrompfrompfrompfrompfromppromage at com.lowagie.processcontent(unlowagagie) extractor.main(extractor.java:64)

编辑: 我已将 try/catch 放入 for 循环中
并添加了堆栈跟踪
并删除索引=1

有帮助吗?

解决方案

愚蠢的问题,而是你在catch把从同一个封装中一个被抛出的ArrayIndexOutOfBoundsException异常?即java.lang中

或许赶上抛出,看看是否能连工作。

其他提示

您正在调用的代码可能正在处理 ArrayIndexOutOfBoundsException 并单独打印堆栈跟踪而不重新抛出它。如果是这样的话,您将看不到您的 System.out.println 叫。

编辑:如果您想继续前进,那么最好知道 PDFContentStreamProcessor#processContent 会抓住 ArrayIndexOutOfBoundsException 然后抛出它的一个实例 com.lowagie.text.ExceptionConverter, ,它是一个子类 RuntimeException.

也许这是一个没有脑子(毕竟,我在3个小时的睡眠,在过去36小时的运转),而是沿着什么digiarnie和ANKUR提到的台词:你尝试过简单catch (Exception e)

这绝对不是理想的,因为很明显这(与Throwable t建议一起)会赶上阳光下的每一个例外,而不是局限于ArrayOutOfBoundsException。只是思想观念在那里,如果你还没有尝试过呢。

而不是使用这个例外的,你应该修正你的代码,这样你就不会走过去的数组边界!

大多数阵列从0计数到array.length-1

如果您更换用这个循环中,你可能会这样避免了整个问题:

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

您需要的try / catch是里面的for循环。控制弹出的尝试捕捉,捕捉火灾,事后恢复控制,但for循环已经结束。

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

也许这是一个愚蠢的问题...你确定异常在您发布的代码,而不是在differen方法抛出?

在程序应该有工作。你应该给予更多的细节,包括你的类名。您可以通过捕捉异常或把一个终于在它的一些s.o.p阻止尝试。

这是奇怪的 - 我真的在异常是从(CMapAwareDocumentFont.decodeSingleCID)抛出的方法来看看的iText的来源和它看起来像这样:

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

它抛出的ArrayIndexOutOfBoundsException异常是标准Java之一。我看不出有任何理由你原来的try-catch不工作。

也许你应该张贴整个类?此外,您使用的iText哪个版本的?

等待第二个!你缺少一些支撑在那里:)你的catch语句超出你的说法!你有这样的:

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

它应该是:

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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top