Handling Exceptions


When an error occurs in either the database or the JDBC driver, a java.sql.SQLException will be raised. The java.sql.SQLException class is a subclass of the java.lang.Exception class. For this reason, you must either place all your JDBC statements within a try / catch statement, or your code must throw a java.sql.SQLException . When such an exception occurs, Java attempts to locate the appropriate handler to process the exception.

If you include a handler for a java.sql.SQLException in a catch clause, when an error occurs in either the database or the JDBC driver, Java will move to that handler and run the appropriate code that you ve included in that catch clause. In this code, you can do things like display the error code and error message, which will help you determine what happened .

The following try / catch statement contains a handler for exceptions of type java.sql.SQLException that may occur in the try statement:

 try {  ... } catch (SQLException e) {  ... } 
Note  

I m assuming java.sql.* has been imported so I can simply use SQLException in the catch, rather than having to reference java.sql.SQLException .

The try statement will contain your JDBC statements that may cause a SQLException to be thrown, and the catch clause will contain your error handling code.

The SQLException class defines four methods that are useful for finding out what caused the exception to occur:

  • getErrorCode()    In the case of errors that occur in the database or the JDBC driver, this method returns the Oracle error code, which is a five-digit number.

  • getMessage()    In the case of errors that occur in the database, this method returns the error message, along with the five-digit Oracle error code. In the case of errors that occur in the JDBC driver, this method returns just the error message.

  • getSQLState()    In the case of errors that occur in the database, this method returns a five-digit code containing the SQL state. In the case of errors that occur in the JDBC driver, this method doesn t return anything of interest.

  • printStackTrace()    This method displays the contents of the stack when the exception occurred. This information may further assist you in finding out what went wrong.

The following try / catch statement illustrates the use of these four methods:

 try {  ... } catch (SQLException e) {  System.out.println("Error code = " + e.getErrorCode());  System.out.println("Error message = " + e.getMessage());  System.out.println("SQL state = " + e.getSQLState());  e.printStackTrace(); } 

If your code throws a SQLException rather than handling it locally as just shown, Java will search for an appropriate handler in the calling procedure or function until one is found. If none are found, the exception will be handled by the default exception handler, which displays the Oracle error code, the error message, and the stack trace.




Oracle Database 10g SQL
Oracle Database 10g SQL (Osborne ORACLE Press Series)
ISBN: 0072229810
EAN: 2147483647
Year: 2004
Pages: 217

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net