我有一个 void 方法,它是 quartz 调度程序任务运行的流程的一部分。
该方法的内容是这段代码:
try {
InputStream ris = this.getClass().getResourceAsStream("arialuni.ttf");
byte[] ttfAfm = new byte[1];
if (ris != null) {
System.out.println("toByteArray START");
ttfAfm = IOUtils.toByteArray(ris);
System.out.println("toByteArray END");
} else
System.out.println("input stream from arailuni.ttf is null!!!");
ris.close();
ris = null;
bfChinese =
BaseFont.createFont("arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, true, ttfAfm, null);
System.out.println("in myinit() try catch END");
} catch (Exception e) {
System.out.println("exception encountered at myinit() " + e);
}
很奇怪,这大约可以运行 30 次(基本上,调度程序扫描一个目录,获取找到的任何 PCL 并使用这种 arialuni.ttf 字体将其转换为 PDF),但突然它给出了以下异常:
19:06:24,316 INFO [STDOUT] toByteArray START
19:06:28,218 ERROR [ReportPollingJob] java.lang.reflect.InvocationTargetExceptio
n
//nothing else here (yes, the exception is only one line...)
在IOUtils.toByteArray(ris)
难道是内存的原因? 很奇怪,它没有去我的捕获,而是抛出这个异常......
能给个提示吗?
更新:感谢 mdma:我已更改为 catch(Throwable e),现在我看到:
java.lang.OutOfMemory: JavaHeap Space
这并不容易解决......
最佳答案
InitationTargetException
只是真正异常的包装器,因此您应该对其进行分析(通过 getCause()
)。它可能是一个 OutOfMemoryError
,它不会子类 Exception
。要捕获每个错误条件,请改为捕获 Throwable
。
关于java - 为什么这段代码会抛出异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5687769/