1

I wrote a piece of code to catch specific exception with help from this link. It is like:

catch (SQLException sqle) {
    // TODO Auto-generated catch block
String sqlMessage = sqle.getMessage();
String sqlState = sqle.getSQLState();
int vendorCode = sqle.getErrorCode();
System.out.println("Exception occurred:");
System.out.println("Message: " + sqlMessage);
System.out.println("SQL state: " + sqlState);
System.out.println("Vendor code: " + vendorCode); 
}

But I am getting output as:

java.sql.SQLException: ORA-00001: unique constraint (SYSTEM.PK_USERID) violated

at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:125)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:305)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:272)
at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:623)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:181)
at oracle.jdbc.driver.T4CPreparedStatement.execute_for_rows(T4CPreparedStatement.java:543)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1028)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:2888)
at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:2960)
at accessdb.Dao.insertnewuser(Dao.java:32)
at Registration.doGet(Registration.java:47)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)

if the error is :

java.sql.SQLException: ORA-00001: unique constraint (SYSTEM.PK_USERID) violated

and I am getting output as

Exception occurred:
Message: Listener refused the connection with the following error:
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
The Connection descriptor used by the client was:
localhost:1521:XE
SQL state: null
Vendor code: 0

if the error is :

Listener refused the connection with the following error: ORA-12505, TNS:listener does not currently know of SID given in connect descriptor The Connection descriptor used by the client was: localhost:1521:XE.

Why is the difference in output happening for same piece of code?

EDIT: Ok, so here is the try block for further clarification.

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // Authentication and Logging in The Registered User
    Getset g=new Getset();
    Dao dao=new Dao();
    String userid="";
    String fname="";
//  PrintWriter pw=response.getWriter();
    String loginemail=request.getParameter("loginemail");
    String loginpassword=request.getParameter("loginpassword");
    if(loginemail.equals("")||loginemail.equals(" ")||loginpassword.equals("")||loginpassword.equals(" "))
    {
        response.sendRedirect("WelcomePage.jsp");
    }
    else{
    g.setloginemail(loginemail);
    g.setloginpassword(loginpassword);
    try {
        ResultSet rs=dao.loginauthentication(g);
        while(rs.next())   //Fetching all emails and passwords from user table
        {
            String regemail=rs.getString("regemail");
            String regpassword=rs.getString("regpassword");
            System.out.println(""+regemail);
            if(loginemail.equals(regemail) && (loginpassword.equals(regpassword))==true)
            {   
                System.out.println("55555");
                ResultSet rs1=dao.getnameid(g);
                while(rs1.next())   //GET USERID and name FROM NEWUSER TO USE AS PRIMARY KEY
                {
                     userid=rs1.getString("USERID");
                     fname=rs1.getString("FNAME");
                //  System.out.println(""+userid);

                }

                HttpSession session = request.getSession(true);
                  session.setAttribute("USERID", userid);
                  session.setAttribute("FNAME", fname);
                response.sendRedirect("UserHome.jsp");
                return;
            }
        }  
        if(rs.next()==false){
            System.out.println("caught");
            response.sendRedirect("WelcomePage.jsp");
            return; 
        }

    }
Mistu4u
  • 5,132
  • 15
  • 53
  • 91
  • 1
    Could you post `insertnewuser` method? – orique Aug 01 '12 at 18:22
  • Actually it is part of a J2EE project.Everything is working fine, I can assure that. Just I want to print the error in the browser to a user if he inserts something wrong. that is why I am experimenting with catching specific exceptions. – Mistu4u Aug 01 '12 at 18:25
  • What exactly is the question? – Jon Lin Aug 01 '12 at 18:32
  • The question with question mark `Why is the difference in output happening for same piece of code?` :) – Mistu4u Aug 01 '12 at 18:34
  • 1
    @JonLin, I am not sure, but I believe the question is basically asking why the output is different for the two `java.sql.SQLException` cases. – Dilum Ranatunga Aug 01 '12 at 18:34
  • Out of curiosity, how are you getting that stack trace if you are catching the exception and not printing a stack trace? – Jon Lin Aug 01 '12 at 18:50
  • Yes, that is also strange...!! I am not printing `stacktrace`. I guess it may be `tomcat` or `eclipse` problem. – Mistu4u Aug 01 '12 at 18:52

3 Answers3

5

You have not posted enough of your code to provide a definite answer, but this is what you'd call an "educated guess": the try of your catch clause does not enclose this line of code:

accessdb.Dao.insertnewuser(Dao.java:32)

With your new code, the following hypothesis is even more plausible: your insertnewuser method has its own try-catch block that looks like this:

try {
  ...prepare an insert statement...
  stmt.executeUpdate(...);
} catch (SQLException e) { e.printStackTrace(); }

This swallows your exception and makes your method complete normally in spite of the exception. Solution: remove the entire try-catch block from that method and make the method declare throws SQLException.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • 1. Now you have `try` with no `catch`. Confusing. 2. There is still no evidence in that code of calling `insertnewuser`. – Marko Topolnik Aug 01 '12 at 19:00
  • got my error. The piece of code for `catch` block should be added to the class where the exception is occuring. I was catching from wrong class.Thanks. – Mistu4u Aug 01 '12 at 19:04
  • No, Subir, that may not be the best recourse. Instead **delete the entire catch block** in `isnertnewuser` and let the exception bubble up! – Marko Topolnik Aug 01 '12 at 19:06
  • Eclipse is not permitting to delete `catch` anyway. :) – Mistu4u Aug 01 '12 at 19:23
  • Eclipse has nothing to do with it, Subir. It just tells you that you have unhandled checked exceptions. Just have your method declare `throws SQLException`, this is one of the two quick-fixes offered. – Marko Topolnik Aug 01 '12 at 19:34
0

There are two issues here. Note that this answer is just a guess since you didn't post enough code.

On one hand, you get the desired output when an SQLException is thrown when trying to connect to the DB server. Probably, it means that you put your catch block in the chunk of code that manages the connection to your DB.

On the other hand, you get an undesired output when trying to insertnewuser and an ORA-00001 error is raised by the DB, which in turn causes an SQLException. That means that the connection to the database has succeeded, but the insert operation has not.

Do you have two try-catch structures?

orique
  • 1,295
  • 1
  • 27
  • 36
0

Let me join the play "let's guess the code" too: you're using some persistence abstraction and not calling flush()/ormFlush()/what have you -> so there's no guarantee of immediate insert -> so the SQLException gets thrown whenever/wherever?

pafau k.
  • 1,667
  • 12
  • 20
  • I can assure you insertion is happening right after a user registers. I am creating the exceptions willingly to catch the exceptions specifically. – Mistu4u Aug 01 '12 at 18:44