Role of SQL Exception object in establishing a con

2019-07-29 13:46发布

问题:

Please consider the following code:

Code Snippet #1

Statement InsertRemoteResultsStmt = null;
Connection connRemote = null;
try {
     connRemote = DriverManager.getConnection("//my urls here");

     // other stuff here
}

catch(SQLException ex)

{

     System.out.println("SQLException: " + ex.getMessage());
     System.out.println("SQLState: " + ex.getSQLState());
     System.out.println("VendorError: " + ex.getErrorCode());

    // For Connection #1 
    InsertRemoteResultsStmt = connRemote.createStatement(); 
    // Rest of the connection code

    // For Connection #2 (Will be a new connection) 

}

OR

Code Snippet #2:

Statement InsertRemoteResultsStmt = null;
Connection connRemote = null;
try {
     connRemote = DriverManager.getConnection("//my urls here");

     // other stuff here
}

catch(SQLException ex) // Original catch block

{

     System.out.println("SQLException: " + ex.getMessage());
     System.out.println("SQLState: " + ex.getSQLState());
     System.out.println("VendorError: " + ex.getErrorCode());

     try{

    // For Connection #1 ( Will be using an existing one)
    InsertRemoteResultsStmt = connRemote.createStatement(); 
    // Rest of the connection code

    // For Connection #2 (Will be a new connection) 
         }

    Catch(SQLException insidecatch)
    {

     }  

     break;
}

Que #1 Is Code Snippet #2 a better way of handling exceptions in my case?

For Next Question:

So, I understood that if I have to display something using the exception object, I can do something like following inside the catch block:

System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());

However, in my case, I am using an existing opened connection inside the catch block as shown above, and I am wondering:

Que #2) Whether the code inside the catch block will start executing automatically as soon as an exception is caught ? I am confused because, in my System.out.println statements, I am using ex object to display all the messages and I am not using this object anywhere in the JDBC connection following it.

Que #3 Is it okay if I use break statement at the end and get out of catch block? Since break is used for loops I believe control will automatically come out of the original catch block?

Clarifications for Que #1:

I am basically comparing two approaches keeping error handling in mind , either of one I am planning to use. In Code Snippet #1 , I have not added any additional try-catch block inside the catch block and vice versa in Code Snippet #2. I am basically using two connections inside catch block. The Connection #1 will be a connectionswhich will be used to insert some records in one database and same with the Connection #2.

Since I have added additional try-catch block inside the catch block, I wanted to know is this approach good to proceed? Just wondering since I have more error handling code here, this approach should be better than theone presented in Code Snippet #1?

回答1:

  1. Neither of them do what you want. I'm not even sure what you want.

  2. Yes. As soon as an exception is thrown from something inside the try block, execution goes straight to the catch block and exits the try block. The ex object is the exception itself, and is generated by whatever code threw the exception.

  3. No. There is no way to go back to just after the exception was thrown, if that's what you're thinking. After the last statement in the catch block, execution goes to the next statement after the catch block (except for other catch blocks).

3a. break is for loops and switch blocks. Breaking out of a catch block would be like breaking out of an if statement.

But I don't think you understand exceptions. An exception is thrown if something goes wrong - it plays no role in establishing the connection. If DriverManager.getConnection throws an SQLException, then something went wrong when it tried to connect to the database, so it didn't connect to the database. If you try to use the database connection anyway, that's a recipe for failure. If it does happen, there's not much you can do apart from displaying an error message and giving up.



标签: java jdbc