ArrayIndexOutOfBoundsException 未被捕获和忽略
我想捕获并忽略 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
您放入 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.
您调用的代码可能正在处理 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 yourSystem.out.println
called.EDIT: If you want to keep chugging along, it would be good to know that the
PDFContentStreamProcessor#processContent
will catch theArrayIndexOutOfBoundsException
and then throw an instance of itscom.lowagie.text.ExceptionConverter
, which is a subclass ofRuntimeException
.也许这是理所当然的(毕竟,我在过去 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 toArrayOutOfBoundsException
. Just thought idea out there if you haven't tried it yet.您应该修复代码,以免超出数组边界,而不是使用此异常!
大多数数组从 0 计数到 array.length-1
如果您用此替换 for 循环,则可能会避免整个问题:
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:
你需要将 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.
也许这是一个愚蠢的问题...您确定异常是在您发布的代码中抛出的,而不是在不同的方法中抛出的吗?
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?
该程序应该有效。您应该提供更多详细信息,包括您的班级名称。您可以尝试捕获异常或放置一个带有一些 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.
这很奇怪 - 我实际上查看了从 (CMapAwareDocumentFont.decodeSingleCID) 抛出异常的方法中的 itext 源代码,它看起来像这样:
它抛出的 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:
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?
等一下!你在那里缺少一些大括号:)你的catch语句在你的for语句之外!你有这个:
它应该是:
Wait a second! You're missing some braces in there :) Your catch statement is outside your for statement! You have this:
It should be: