Return method with throw

Asked

Viewed 152 times

1

This method has the type of return Connection, however in the blocks catch there is no return conn and still there is no compilation or execution error. I believe it is by throws and throw but I don’t know exactly why.

    public static Connection getConexao() throws SQLException, ClassNotFoundException {

    Connection conn = null;
    try {
        Class.forName(DRIVER_CONEXAO);
        conn = DriverManager.getConnection(STR_CONEXAO + DATABASE, USUARIO, SENHA);

        return conn;

    } catch (ClassNotFoundException e) {
        throw new ClassNotFoundException(
                "Driver MySql não foi encontrado " + e.getMessage());

    } catch (SQLException e) {
        throw new SQLException("Erro ao conectar "
                + "com a base de dados" + e.getMessage());
    }
}
  • 1

    Fell into the catch, means that some problem has occurred and the action within the try was not executed, so the exception is released. If you did not create the connection for some reason, how will you create it? The purpose of the exception is to inform you which problem occurred and where it occurred so that you can subsequently correct.

  • 1

    And another, relaunching connection exception onwards is not a good practice. Either you create a custom exception to pass the error message on or boom (I think this option is a little "nut") as a RunTimeException and handles further so that the message is displayed in a friendly way. Another thing, it doesn’t make much sense to put Try/catch and throw throws for the same exceptions.

1 answer

1


It is because of the assumption that you are in the question and also because you are making another exception (which is unusual and probably wrong to do). If an exception is made, the return is not necessary. If you take this exception, neither compile.

See the "running" example in ideone. And see or compile. I put in the Github for future reference.

Anyway if this is not just an example, it is the wrong way to use exception. Read on the subject at tag . There are numerous questions on the subject where, me and other users here talk about the right way to use exceptions, and how most programmers misuse exceptions.

  • Is relaunching wrong? But I usually relaunch a custom exception when a bank error occurs and treat the problem in the class itself, because my views and controllers classes don’t need to know what to do with an exception that is the responsibility of the connection class. Not this line of reasoning to follow: each class with its own responsibility?

  • Yes, it is wrong in most situations. In Java not so much because it abuses exceptions, so there are many cases, there is no other way. You lose information from the stack and think the mistake is in a different place than it really is. Of course for everything it has exception (with the pardon of the pun). Each class with its responsibility, of course. There are cases where it’s okay to do this, you seem to know this. This specific example of the question is that it is wrong and you have noted this in your comment.

  • I understand, you were referring to problems such as the question, not generalized in language, I believe there was a mistake in my interpretation.

  • 1

    There are also widespread problems, yes, but I was talking more about the question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.