Display specific error! java.sql.Sqlexception'

Asked

Viewed 720 times

1

Hello

I am as following code:

<c:set var="exception" value="${requestScope['java.sql.SQLException']}"/>
    <p class="alert alert-danger"><b>Ops!</b> Erro: <br><br>
 <!-- Stack trace -->

    <jsp:scriptlet>
      // exception.printStackTrace();
      exception.printStackTrace(new java.io.PrintWriter(out));
    </jsp:scriptlet>
   </p>

It’s just that mistakes come a lot:

 java.sql.SQLException: ORA-20999: Aluno já cadastrado anteriormente
 ORA-06512: em "DBAADV.TRG_CURSO_INSCRICAO", line 8 
 ORA-04088: erro durante a execução do gatilho 'DBAADV.TRG_CURSO_INSCRICAO' 
 ORA-06512: em "DBAADV.PROC_INSCRICAO", line 13 
 ORA-06512: em line 1 at 
 oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:450) at 
 oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:399) at 
 oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:1059) at 
 oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:522) at 
 oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:257) at 
 oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:587) at 

I’d just like to pick up the specific error:

ORA-20999: Aluno já cadastrado anteriormente

or

Aluno já cadastrado anteriormente

My Try/catch

try {
             ...           
        } catch (SQLException ex) {
            throw new ServletException(ex);

        }

1 answer

1

The console is showing a lot of information because you are displaying all the exception stacktrace.

Instead, what you are looking for is the detailed exception message, which can be accessed through the getMessage() or getLocalizedMessage() if you want the error message to be located.

The error code (ORA-2099 in your example) can also be obtained through the getSQLState().

My biggest question (especially reading the comments) and the edited question is why you want to print exception information through your JSP page. JSP content should, in general, consist only of elements that will compose what is processed in clients.

The handling of the exception you wish to my view is part of the server scope, and it would be much easier to include it along with your catch through a System.out.println() passing as parameter the message/cause/code and any other details of the exception that interest you, or better still: use your Logger to print according to your settings l4j.

  • With getMessage() and getLocalizedMessage() nothing appeared and with getSQLState() did cannot find symbol

  • Nothing showed up on the console, but that doesn’t mean the messages are empty. You still need to write these variables in your default output (terminal), which you did before through the new java.io.PrintWriter(out). There are several ways to send this message pro terminal. One of them is to use js through console.log(""), that will display in the terminal everything that is passed as parameter.

  • I tried so: exception.getMessage();

  • The printStackTrace problem is that it will display the entire stack of errors, which includes more information than you want to see. The methods getMessage() do not redirect the output by default, IE, you will still need to send this information to the default output of your server, and there are a few different ways to do this. This may appear on the client (browser) console via console.log(); or on the server using <% out.println(Exception.getMessage()) %>. Another better solution would be to control what is displayed on the console in its server access method.

  • the control would be more manual?

  • and if you wanted to take only the first message?

  • This error page will not always be the same message. it comes automatically, but I would like it to show in a friendly way

  • Ah, just now I understand what you want to do! You want to display the exception message on your JSP page! Well, in that case I think the best practice would be for you to create your own exceptions and have them extend Servletexception. Then you could make a robust exception control and fire/treat them correctly. throw new ServletException(ex); for throw new ServletException(ex.getMessage());, I believe that the constructor you are using is swallowing the exception message and so will empty.

Show 3 more comments

Browser other questions tagged

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