Handling errors v42.7.3.1
When connecting to an external resource (such as a database server), errors are bound to occur. Your code must include a way to handle these errors. Both JDBC and the EDB Postgres Advanced Server JDBC Connector provide various types of error handling. The ListEmployees class example shows how to handle an error using try/catch
blocks.
When a JDBC object returns an error (an object of type SQLException
or of a type derived from SQLException
), the SQLException
object exposes three different pieces of error information:
- The error message
- The SQL state
- A vendor-specific error code
In this example, the code displays the value of these components if an error occurs:
For example, if the server tries to connect to a database that doesn't exist on the specified host, the following error message is displayed: