【发布时间】:2014-03-03 14:24:10
【问题描述】:
我想创建一个方法来测试不同的事物并根据问题抛出错误(然后退出程序)。 我对抛出异常不是很熟悉......这种方法是一种编程的好方法吗?
private static void testConnexions() throws IOException, Exception {
File file = null;
Socket socket;
try {
// Test access to the repository:
file = new File(pdfRepository);
if (!file.canRead())
throw new SecurityException();
if (!file.canWrite())
throw new SecurityException();
if (!file.exists())
throw new SecurityException();
if (!file.isDirectory())
throw new SecurityException();
// Connection to Filenet:
connexion = new FilenetConnexion(filenetURI, objectStoreName,
stanza, dossierRacine, userName, password);
connexion.connect();
// Connection to a socket:
socket = new Socket(serveurCV, portCV);
// Initialize the JavaMail Session:
Properties props = System.getProperties();
if (serveurSMTP != null)
props.put("mail.smtp.host", serveurSMTP);
Session session = Session.getInstance(props, null);
} catch (SecurityException e) {
e.getMessage();
e.printStackTrace();
} catch (UnknownHostException e) {
e.getMessage();
e.printStackTrace();
} catch (IOException e) {
e.getMessage();
e.printStackTrace();
} catch (Exception e) {
e.getMessage();
e.printStackTrace();
}
}
我想捕获一条足够详细的消息,以了解是否无法写入存储库,或者 System.getProperties() 是否出现错误等。
提前感谢您的帮助!
编辑: 这是我在您的所有贡献中选择的解决方案,希望它可以帮助某人:
private static void testConnexions() {
File file = null;
Socket socket;
// Test access to the repository:
try {
file = new File(pdfRepository);
if (!file.canRead())
throw new SecurityException(pdfRepository + " can't be read.");
if (!file.canWrite())
throw new SecurityException(pdfRepository + " can't be written.");
if (!file.exists())
throw new SecurityException(pdfRepository + " doesn't exist.");
if (!file.isDirectory())
throw new SecurityException(pdfRepository + " isn't a folder.");
} catch (SecurityException e) {
logger.error(e.getMessage());
System.exit(1);
}
// Connection to FileNet
try {
connexion = new FilenetConnexion(filenetURI, objectStoreName,
stanza, dossierRacine, userName, password);
connexion.connect();
} catch (Exception e) {
logger.error("Impossible to connect to FileNet. " + e.getMessage());
System.exit(2);
}
// Connection to FrontalSocket
try {
socket = new Socket(serveurCV, portCV);
} catch (UnknownHostException e) {
logger.error("Impossible to connect to FrontalSocket. " + e.getMessage());
System.exit(3);
} catch (IOException e) {
logger.error("Impossible to connect to FrontalSocket. " + e.getMessage());
System.exit(3);
}
// Initialize the JavaMail session
try {
Properties props = System.getProperties();
if (serveurSMTP != null)
props.put("mail.smtp.host", serveurSMTP);
else{
logger.error("The SMTP host name is null");
System.exit(4);
}
Session session = Session.getInstance(props, null);
} catch (Exception e) {
logger.error("Impossible to connect to SMTP server. " + e.getMessage());
System.exit(4);
}
}
【问题讨论】:
-
不!您抛出的所有 SecurityException 都将被捕获在空的 catch-all
catch Exception块中。 -
糟糕,抱歉,忘记填写了。
-
既然您提到您不熟悉抛出异常,请立即停下来阅读入门级Java书籍。您将学习如何使用异常和许多其他东西。
-
顺便说一句,由于您已经在代码中捕获了
Exception和IOException,因此您的方法将永远不会抛出它们。这意味着您可以从方法中删除throws IOException, Exception子句。这仅适用于您不想捕获的异常。