【问题标题】:Multiple tests with multiple throws多次抛出的多次测试
【发布时间】: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书籍。您将学习如何使用异常和许多其他东西。
  • 顺便说一句,由于您已经在代码中捕获了 ExceptionIOException,因此您的方法将永远不会抛出它们。这意味着您可以从方法中删除 throws IOException, Exception 子句。这仅适用于您不想捕获的异常。

标签: java testing throw


【解决方案1】:

您可以通过多种方式做到这一点,选择最适合您的场景的一种:

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

案例一的例子:

首先定义你自己的异常:

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

【讨论】:

    【解决方案2】:

    在这种情况下,我可以建议抛出一个用户定义的异常,并传递一个足够详细的消息,以了解导致该错误的原因。

    public class MyException extends Exception {
        //override the needed methods.
    }
    

    然后抛出您自己定义的异常,并附上详细的消息。

    try {
        // Test access to the repository:
        file = new File(pdfRepository);
        if (!file.canRead())
            throw new MyException("The fine has no read permission");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-08
      • 2015-10-30
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 2023-02-23
      相关资源
      最近更新 更多