一、数据为 null
java
private static byte[] getData() {
return null;
}
java
byte[] data = getData();
try (PDDocument document = PDDocument.load(data)) {
} catch (IOException e) {
e.printStackTrace();
}
-
异常未被捕获,目标捕获的异常是 IOException,实际的异常是 NullPointerException,输出结果如下
Exception in thread "main" java.lang.NullPointerException: Cannot read the array length because "input" is null
二、数据为空
java
private static byte[] getData() {
return new byte[0];
}
java
byte[] data = getData();
try (PDDocument document = PDDocument.load(data)) {
} catch (IOException e) {
e.printStackTrace();
}
-
异常被捕获,输出结果如下
java.io.IOException: Error: End-of-File, expected line at offset 0
三、数据异常(PDF 文件损坏)
java
private static byte[] getData() {
try {
File pdfFile = new File("pdf/example.pdf");
InputStream pdfInputStream = new FileInputStream(pdfFile);
ByteArrayOutputStream pdfBuffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[1024];
while ((nRead = pdfInputStream.read(data, 0, data.length)) != -1) {
pdfBuffer.write(data, 0, nRead);
}
byte[] bytes = pdfBuffer.toByteArray();
for (int i = 0; i < 100; i++) {
bytes[i] = (byte) (bytes[i] + 1);
}
return bytes;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
java
byte[] data = getData();
try (PDDocument document = PDDocument.load(data)) {
} catch (IOException e) {
e.printStackTrace();
}
-
异常被捕获,输出结果如下
java.io.IOException: Error: Header doesn't contain versioninfo
四、数据异常(非 PDF 文件)
java
private static byte[] getData() {
try {
File pdfFile = new File("image/test.jpeg");
InputStream pdfInputStream = new FileInputStream(pdfFile);
ByteArrayOutputStream pdfBuffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[1024];
while ((nRead = pdfInputStream.read(data, 0, data.length)) != -1) {
pdfBuffer.write(data, 0, nRead);
}
byte[] bytes = pdfBuffer.toByteArray();
for (int i = 0; i < 100; i++) {
bytes[i] = (byte) (bytes[i] + 1);
}
return bytes;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
java
byte[] data = getData();
try (PDDocument document = PDDocument.load(data)) {
} catch (IOException e) {
e.printStackTrace();
}
java.io.IOException: Error: Header doesn't contain versioninfo
五、文件为 null
java
private static File getFile() {
return null;
}
java
File file = getFile();
try (PDDocument document = PDDocument.load(file)) {
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("next");
-
异常未被捕获,目标捕获的异常是 IOException,实际的异常是 NullPointerException,输出结果如下
Exception in thread "main" java.lang.NullPointerException
六、文件不存在
java
private static File getFile() {
return new File("pdf/test123.pdf");
}
java
File file = getFile();
try (PDDocument document = PDDocument.load(file)) {
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("next");
-
异常被捕获,输出结果如下
java.io.FileNotFoundException: pdf\test123.pdf (系统找不到指定的文件。)
七、文件异常(非 PDF 文件)
java
private static File getFile() {
return new File("image/test.jpeg");
}
java
File file = getFile();
try (PDDocument document = PDDocument.load(file)) {
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("next");
-
异常被捕获,输出结果如下
java.io.IOException: Error: Header doesn't contain versioninfo