Display specific error! java.sql.SQLException'

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>

Only the error comes 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 would just like to catch 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);

        }
Author: adventistaam, 2017-05-17

1 answers

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

Instead, what you are looking for is the detailed exception message, which can be accessed through 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 getSQLState().

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

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

 0
Author: fwerther, 2017-05-17 15:01:44