Error handling
The Java SDK provides a structured exception hierarchy for handling errors from the database and SDK. All exceptions extend SurrealException, which is an unchecked exception. Server-returned errors are represented as ServerException subclasses with typed error details.
API References
| Error class | Description |
|---|---|
SurrealException | Base exception for all SDK errors |
ServerException | Base for server-returned errors |
NotFoundException | Thrown when a resource is not found |
NotAllowedException | Thrown when an operation is not permitted |
QueryException | Thrown when a query fails |
AlreadyExistsException | Thrown when a resource already exists |
Exception hierarchy
SurrealException is the base class for all exceptions thrown by the SDK. It extends RuntimeException, so exceptions are unchecked. ServerException extends SurrealException and represents errors returned by the SurrealDB server. Specific exception types extend ServerException for common error categories.
SurrealException— base for all SDK errorsServerException— base for server-returned errorsNotFoundException— resource not foundNotAllowedException— operation not permittedQueryException— query execution failedAlreadyExistsException— resource already exists
Catching server errors
All server errors are ServerException subclasses. Catch specific exceptions first for targeted handling, then fall back to ServerException for unexpected server errors.
Inspecting error details
ServerException provides methods for inspecting the error returned by the server.
.getKind()— returns the error kind as aString.getKindEnum()— returns the error kind as anErrorKindenum value.getDetails()— returns additional error details
The ErrorKind enum includes: VALIDATION, CONFIGURATION, THROWN, QUERY, SERIALIZATION, NOT_ALLOWED, NOT_FOUND, ALREADY_EXISTS, CONNECTION, INTERNAL, and UNKNOWN.
Traversing error chains
Server errors can have nested causes. ServerException provides methods for walking the cause chain to find a specific error type.
.getServerCause()— returns the underlyingServerExceptioncause, if any.hasKind(kind)— checks whether this error or any cause matches the givenErrorKind.findCause(kind)— searches the cause chain and returns the firstServerExceptionmatching the givenErrorKind
Handling specific error types
Specific exception subclasses expose additional context about the error.
NotFoundException provides .getTableName() and .getRecordId() to identify the missing resource.
NotAllowedException provides .isTokenExpired() and .isInvalidAuth() to distinguish authentication failures.
QueryException provides .isTimedOut() and .isCancelled() to identify query lifecycle issues.
Learn more
Errors API reference for complete exception class documentation
Connecting to SurrealDB for connection error scenarios
Authentication for authentication error scenarios
SurrealQL THROW for throwing custom errors from queries