Displaying error messages in a web application

Asked

Viewed 2,214 times

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.

1 answer

3

It’s all a matter of common sense, Geison. I’m not sure I understand your question exactly, but let’s go in parts.

how best to display the exceptions for that case specific? It is correct to take exceptions and customize your messages as in the example below?

Perhaps an error message in connection with the database is not interesting to the user. He doesn’t want to know technically what happened, many people don’t even know what a database is, for example. So in terms of the application error log, the more specific it is, the better, because we developers have access to it. With regard to what will appear to the user we have to evaluate the following: is an error that should be dealt with or a "choke", an unexpected mistake?

In the first case, at the beginning of a development, we usually don’t have all the possible mistakes. Even the exception treatment goes through a period of maturity during software development. As we develop and come across mistakes, we treat it and the user has a friendly error message, whether it is an unfulfilled mandatory field or an unfulfilled business rule. This type of message should usually be displayed prominently on the screen the user is on.

In the second case, what I particularly do, is treat as "fatal error". I display a standard system error screen.

In general, I have a kind of exception for the system, which I use to "map" known errors, which must be handled. In a barrier of exception I capture this type of exception and display to the user. Any other exception that is still unknown or is the result of a "choking" of the system, is displayed on a unique error screen.

I was able to clear up your doubt?

Hugs,

  • 1

    Pepper I will edit my question to be more clear what I want. I think my question has become very superficial.

  • @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>.

  • "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.

Browser other questions tagged

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