1
I have developed a simple web application to study some concepts related to Servlet
among others. I mapped the file web.xml
of my application to display a página de erro
in case an exception occurs.
Doubt: what is the best way to display the exceptions for this specific case? It is correct to take the exceptions and customize your messages as in the example below?
For a failed connection to the database:
public Connection getConnection() {
try {
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
return DriverManager.getConnection(
"jdbc:mysql://localhost:3306/MYDB", "root", "");
} catch (SQLException e) {
throw new ConnectionException("Falha na conexão com o banco de dados");
}
}
There is good practice regarding the presentation of error messages to the user?
EDIT
Here is my error-handling page (a bit silly yet, but waiting for help to become more serious):
<html>
<body>
<h1>Falha do sistema</h1>
Descrição: ${pageContext.errorData.throwable.message}
</body>
</html>
In that case, (1)
I will need to give Try/catch always in order to customize the exception message (throw new Minhaexception("My message"))? (2)
How I can make my messages dynamic without having to give Try/catch? (3)
Is there any way I can intercept the exceptions and present a different message for each type?
OBSERVING: important to realize that doubt arises from the use of the variable ${pageContext.errorData.throwable.message}
to print the error.
Pepper I will edit my question to be more clear what I want. I think my question has become very superficial.
– Geison Santos
@Geisonsantos, as you want to throw exceptions is not necessary to do the Try/catch, you can simply, where necessary, throw your exception
throw new MinhaExcecao("Minha mensagem!")
. If she goes checked, you will have to at some point use the Try/catch, otherwise you won’t need to. You can create a Servlet filter to intercept your exceptions and treat them the way you want, or if you solve, use the tag<error-page>
in the web xml., in which you have the options to map an error by code<error-code>
or type<exception-type>
and call a jsp or another Servlet<location>
.– Pimenta
"You can create a Servlet filter to intercept your exceptions and treat them the way you want." @Pepper, can you give me an example of this approach? Better, you can add in your answer. Then I close the topic as solved.
– Geison Santos