Skip to content

Latest commit

 

History

History
47 lines (29 loc) · 2.96 KB

handling-errors.md

File metadata and controls

47 lines (29 loc) · 2.96 KB
title description author ms.author ms.date ms.service ms.subservice ms.topic
Handling errors
Learn about error handling and what information the SQLServerException class provides in the Microsoft JDBC Driver for SQL Server.
David-Engel
davidengel
05/01/2023
sql
connectivity
conceptual

Handling errors

[!INCLUDEDriver_JDBC_Download]

When using the [!INCLUDEjdbcNoVersion], all database error conditions are returned to your Java application as exceptions using the SQLServerException class. The following methods of the SQLServerException class are inherited from java.sql.SQLException and java.lang.Throwable; and they can be used to return specific information about the [!INCLUDEssNoVersion] error that has occurred:

  • getSQLState() returns the standard X/Open or SQL99 state code of the exception.

  • getErrorCode() returns the specific database error number.

  • getMessage() returns the full text of the exception. The error message text describes the problem, and frequently includes placeholders for information, such as object names, that are inserted in the error message when it's displayed.

  • getSQLServerError() returns the SQLServerError object containing detailed info about the exception as received from SQL Server. This method returns null if no server error has occurred.

The following methods of the SQLServerError class can be used to obtain more details about the error generated from the server.

  • SQLServerError.getErrorMessage() returns the error message as received from the server.

  • SQLServerError.getErrorNumber() returns a number that identifies the type of the error.

  • SQLServerError.getErrorState() returns a numeric error code from SQL Server that represents an error, warning or "no data found" message.

  • SQLServerError.getErrorSeverity() returns the severity level of the error received.

  • SQLServerError.getServerName() returns the name of the computer that is running an instance of SQL Server that generated the error.

  • SQLServerError.getProcedureName() returns the name of the stored procedure or remote procedure call (RPC) that generated the error.

  • SQLServerError.getLineNumber() returns the line number within the Transact-SQL command batch or stored procedure that generated the error.

In the next example, an open connection to the [!INCLUDEssNoVersion] [!INCLUDEssSampleDBnormal] sample database is passed in to the function and a malformed SQL statement is constructed that doesn't have a FROM clause. Then, the statement is run and an SQL exception is processed.

[!codeJDBC#HandlingErrors1]

See also

Diagnosing problems with the JDBC driver