Search code examples
javatestingthrow

Multiple tests with multiple throws


I would like to create a method that will test different things and throw an error depending on the issue (and then exit the program). I am not really familiar with throwing exception... Is this method a good way to program it?

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

I would like to catch a message detailed enough to know if the repository can't be written in, or if the System.getProperties() got an error, etc.

Thank you in advance for your help!

EDIT: Here is the solution I chose among all your contributions, hoping it can help someone:

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

Solution

  • You can do this several ways, choose which one fits your scenario best:

    • Throw different exceptions based on each error scenario. It is easy to subclass Exception and create the distinction this way.
    • Throw the same exception with a specific error message depending on the error scenario.

    An example of case 1:

    First define your own exceptions:

    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
    }
    

    Then throw and catch them:

    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
    }
    

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