Exception handling is an important part of Java programming, especially when
connecting to or running a query against a back-end relational database.
SQLException is the class that we have been using to indicate database
related errors. JDBC 4.0 has several enhancements in SQLException
handling. The following are some of the enhancements made in JDBC 4.0 release to
provide a better developer's experience when dealing with SQLExceptions:
SQLException sub-classesThe new subclasses of SQLException were created to provide a
means for Java programmers to write more portable error-handling code. There are
two new categories of SQLException introduced in JDBC 4.0:
Non-Transient Exception: This exception is thrown when a
retry of the same JDBC operation would fail unless the cause of the
SQLException is corrected. The new exception classes that are added in
JDBC 4.0 as subclasses of SQLNonTransientException ,which are given below
:
SQLFeatureNotSupportedException SQLNonTransientConnectionException SQLDataException SQLIntegrityConstraintViolationException SQLInvalidAuthorizationException SQLSyntaxErrorExceptionTransient Exception: This exception is thrown when a
previously failed JDBC operation might be able to succeed when the operation is
retried without any intervention by application-level functionality. The new
exceptions extending SQLTransientException are listed below :
SQLTransientConnectionExceptionSQLTransactionRollbackExceptionSQLTimeoutExceptionThe SQLException class now supports the Java SE chained
exception mechanism (also known as the Cause facility), which gives us the
ability to handle multiple SQLExceptions (if the back-end database
supports a multiple exceptions feature) thrown in a JDBC operation. This
scenario occurs when executing a statement that may throw more than one
SQLException .
We can use getNextException() method in SQLException
to iterate through the exception chain. Here's some sample code to process
SQLException causal relationships:
|
3.Support for enhanced for-each loop
In the below example, "Example.java"
, specifying "student4" as a table name that does
not exist in the database. It raised a chained exception as follows:
|
OUTPUT
|
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.