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());
}
}
Fell into the
catch
, means that some problem has occurred and the action within thetry
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.– user28595
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.– user28595