我有一些调用第三方专有库的 java 代码:
public String decrypt(String inputString) {
return ThirdPartyDecrypter.decrypt(inputString);
}
上面的代码编译并为 99% 的用户工作。但是,如果我故意提交错误的不正确输入字符串,则上述方法会抛出 java.io.StreamCorruptedException。
我想捕获该异常并对错误情况执行其他操作:
public String decrypt(String inputString) {
try {
return ThirdPartyDecrypter.decrypt(inputString);
} catch (StreamCorruptedExcepton streamException) {
System.out.println("case streamException");
streamException.printStackTrace(); // does not execute
throw MyNewException(streamException);
} catch (Exception e) {
System.out.println("case e");
e.printStackTrace();
throw MyNewException(streamException);
}
}
当我运行上面的代码时,StreamCorruptedException 没有被捕获,它仍然被抛出而不是 MyNewException。
我需要做什么才能捕获 StreamCorruptedException?我还读过 StreamCorruptedException 应该是一个检查异常。那么第三方库是如何抛出它的(因为他们没有在他们的 API 中声明任何抛出)?