【发布时间】:2014-10-14 11:36:54
【问题描述】:
我想介绍 getKeyStore() 方法,但我不知道如何介绍 NoSuchAlgorithmException、KeyStoreException、UnrecoverableKeyException 和 CertificateException 的 catch 块。我的方法是:
public static KeyManagerFactory getKeyStore(String keyStoreFilePath)
throws IOException {
KeyManagerFactory keyManagerFactory = null;
InputStream kmf= null;
try {
keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keystoreStream = new FileInputStream(keyStoreFilePath);
keyStore.load(keystoreStream, "changeit".toCharArray());
kmf.init(keyStore, "changeit".toCharArray());
} catch (NoSuchAlgorithmException e) {
LOGGER.error(ERROR_MESSAGE_NO_SUCH_ALGORITHM + e);
} catch (KeyStoreException e) {
LOGGER.error(ERROR_MESSAGE_KEY_STORE + e);
} catch (UnrecoverableKeyException e) {
LOGGER.error(ERROR_MESSAGE_UNRECOVERABLEKEY + e);
} catch (CertificateException e) {
LOGGER.error(ERROR_MESSAGE_CERTIFICATE + e);
} finally {
try {
if (keystoreStream != null){
keystoreStream.close();
}
} catch (IOException e) {
LOGGER.error(ERROR_MESSAGE_IO + e);
}
}
return kmf;
}
我该怎么做?
【问题讨论】:
-
我不确定它是否需要覆盖。但是,也许我会创建
handleException(Exception)或者如果我使用的是 java7,也许我会捕获多个异常 (docs.oracle.com/javase/7/docs/technotes/guides/language/…)。我不确定这个日志记录是否值得努力。
标签: java junit try-catch mockito