java ArrayIndexOutOfBoundsException 未被捕获和忽略
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1753752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ArrayIndexOutOfBoundsException not being caught and ignored
提问by Ankur
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).
我想捕获并忽略 ArrayIndexOutOfBoundsException 错误(基本上这不是我可以控制的,所以我需要我的程序继续前进)。
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.
但是,我的 try/catch 对似乎没有捕获异常并忽略它。希望你能找出我做错了什么。
The exception occurs at this line
异常发生在这一行
content = extractor.getTextFromPage(page);
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)
线程“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 (未知来源)在 com.lowagie.text.pdf.parser.PdfContentStreamProcessor.invokeOperator(未知来源)在 com.lowagie.text.pdf.parser.PdfContentStreamProcessor.processContent(未知来源)在 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
编辑:我已经将 try/catch 放在 for 循环中
并添加了堆栈跟踪
并删除了 index=1
采纳答案by digiarnie
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
愚蠢的问题,但是 ArrayIndexOutOfBoundsException 是您从与抛出的那个包相同的包中放入捕获的吗?即java.lang
Or perhaps catch throwable to see if that even works.
或者也许可以捕获 throwable 以查看它是否有效。
回答by akf
It is possible that the code that you are calling is handling the ArrayIndexOutOfBoundsExceptionand printing the the stack trace on its own without rethrowing it. If that is the case, you would not see your System.out.printlncalled.
您正在调用的代码可能正在处理ArrayIndexOutOfBoundsException和打印堆栈跟踪,而无需重新抛出它。如果是这种情况,您将看不到您的System.out.println电话。
EDIT: If you want to keep chugging along, it would be good to know that the PDFContentStreamProcessor#processContentwill catch the ArrayIndexOutOfBoundsExceptionand then throw an instance of its com.lowagie.text.ExceptionConverter, which is a subclass of RuntimeException.
编辑:如果你想继续前进,最好知道PDFContentStreamProcessor#processContent将捕获ArrayIndexOutOfBoundsException并抛出它的实例com.lowagie.text.ExceptionConverter,它是 的子类RuntimeException。
回答by Magsol
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)?
也许这是一个明智的选择(毕竟,我在过去 36 小时内只睡了 3 小时),但正如 digiarnie 和 Ankur 提到的那样:你试过简单catch (Exception e)吗?
It's definitely not ideal, since obviously it (along with the Throwable tsuggestion) will catch every exception under the sun, not limited to ArrayOutOfBoundsException. Just thought idea out there if you haven't tried it yet.
这绝对不是理想的,因为显然它(连同Throwable t建议)会在阳光下捕获所有异常,不限于ArrayOutOfBoundsException. 如果你还没有尝试过,就想出主意。
回答by Matt
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
大多数数组从 0 计数到 array.length-1
If you replace your for loop with this, you might this avoids the entire issue:
如果你用这个替换你的 for 循环,你可能会避免整个问题:
for (int page = 0;page < noPages;page++){
回答by SimonDever
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");
}
}
回答by kolosy
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.
您需要将 try/catch 放在 forloop 内。控制弹出到 try catch,catch 触发,然后恢复控制,但 forloop 已经终止。
回答by TofuBeer
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?
也许这是一个愚蠢的问题......你确定异常是在你发布的代码中而不是在不同的方法中抛出的吗?
回答by fastcodejava
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.
该程序应该有效。您应该提供更多详细信息,包括您的班级名称。您可以尝试通过捕获 Exception 或在其中放置一个带有一些 sop 的 finally 块。
回答by JMM
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:
这很奇怪 - 我实际上在从 (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");
}
The ArrayIndexOutOfBoundsException it throws is the standard Java one. I can't see any reason your original try-catch not working.
它抛出的 ArrayIndexOutOfBoundsException 是标准的 Java 异常。我看不出有任何原因您原来的 try-catch 不起作用。
Perhaps you should post the entire class? Also, which version of itext are you using?
也许你应该发布整个课程?另外,你用的是哪个版本的itext?
回答by JMM
Wait a second! You're missing some braces in there :) Your catch statement is outside your for statement! You have this:
等一等!你在那里缺少一些大括号:) 你的 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");
}
}
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

