gpt4 book ai didi

java - 多次测试多次抛出

转载 作者:行者123 更新时间:2023-11-28 20:20:29 24 4
gpt4 key购买 nike

我想创建一个方法来测试不同的东西并根据问题抛出错误(然后退出程序)。我不太熟悉抛出异常...这种方法是编程的好方法吗?

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);
}
}

最佳答案

您可以通过多种方式执行此操作,选择最适合您的情况的一种:

  • 根据每种错误情况抛出不同的异常。很容易继承 Exception 并以这种方式创建区别。
  • 根据错误场景抛出带有特定错误消息的相同异常。

情况 1 的示例:

首先定义你自己的异常:

public class CannotReadException extends Exception {
// This is a separate class in your project
}
public class CannotWriteException extends Exception {
// This is a separate class in your project
}

然后抛出并接住它们:

try {
// Test access to the repository:
file = new File(pdfRepository);
if (!file.canRead())
throw new CannotReadException();
if (!file.canWrite())
throw new CannotWriteException();
...
} catch (CannotReadException e) {
// Do stuff for the specific error
} catch (CannotWriteException e) {
// Do stuff for the specific error
}

或者,情况 2:

try {
// Test access to the repository:
file = new File(pdfRepository);
if (!file.canRead())
throw new SecurityException( "cannot read" );
if (!file.canWrite())
throw new SecurityException( "cannot write" );
...
} catch (SecurityException e) {
// Get to your specific message using e.getMessage();
}

关于java - 多次测试多次抛出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22149201/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com