ArrayIndexOutOfBoundsException 未被捕获和忽略

发布于 2024-08-12 00:57:34 字数 1366 浏览 6 评论 0原文

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

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

异常发生在这一行

content = 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(来源未知) 在 com.lowagie.text.pdf.CMapAwareDocumentFont.decode(来源未知) 在 com.lowagie.text.pdf.parser.PdfContentStreamProcessor.decode(来源未知) 在 com.lowagie.text.pdf.parser.PdfContentStreamProcessor.displayPdfString(来源未知) 在 com.lowagie.text.pdf.parser.PdfContentStreamProcessor$ShowText.invoke(来源未知) 在 com.lowagie.text.pdf.parser.PdfContentStreamProcessor.invokeOperator(来源未知) 在 com.lowagie.text.pdf.parser.PdfContentStreamProcessor.processContent(来源未知) 在 com.lowagie.text.pdf.parser.PdfTextExtractor.getTextFromPage(来源未知) 在 com.pdfextractor.main.Extractor.main(Extractor.java:64)

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

且行且努力 2024-08-19 00:57:34

您放入 catch 中的 ArrayIndexOutOfBoundsException 是否与抛出的异常来自同一个包?即 java.lang

或者也许 catch throwable 看看是否有效。

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.

爱的故事 2024-08-19 00:57:34

您调用的代码可能正在处理 ArrayIndexOutOfBoundsException 并自行打印堆栈跟踪而不重新抛出它。如果是这种情况,您将不会看到 System.out.println 被调用。

编辑:如果您想继续前进,最好知道 PDFContentStreamProcessor#processContent 将捕获 ArrayIndexOutOfBoundsException,然后抛出其 com.lowagie.text.ExceptionConverter,它是 RuntimeException

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.

魂归处 2024-08-19 00:57:34

也许这是理所当然的(毕竟,我在过去 36 小时内只睡了 3 个小时),但按照 digiarnie 和 Ankur 提到的那样:您是否尝试过简单的 catch (Exception e) ?

这绝对不理想,因为显然它(以及 Throwable t 建议)将捕获所有异常,而不仅限于 ArrayOutOfBoundsException。如果你还没有尝试过的话,只是想一下。

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.

萌逼全场 2024-08-19 00:57:34

您应该修复代码,以免超出数组边界,而不是使用此异常!

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

如果您用此替换 for 循环,则可能会避免整个问题:

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

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++){
忆离笙 2024-08-19 00:57:34

你需要将 try/catch 放在 forloop 中。控制弹出到 try catch,catch 触发,然后恢复控制,但 forloop 已经终止。

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.

滿滿的愛 2024-08-19 00:57:34
    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");
        }
    }
    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");
        }
    }
没︽人懂的悲伤 2024-08-19 00:57:34

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

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?

还在原地等你 2024-08-19 00:57:34

该程序应该有效。您应该提供更多详细信息,包括您的班级名称。您可以尝试捕获异常或放置一个带有一些 sop 的finally 块。

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.

无尽的现实 2024-08-19 00:57:34

这很奇怪 - 我实际上查看了从 (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?

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?

只是一片海 2024-08-19 00:57:34

等一下!你在那里缺少一些大括号:)你的catch语句在你的for语句之外!你有这个:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文