public class DatabaseEncryptionException

  1. Object
  2. Throwable
  3. Exception
  4. IOException
  5. DatabaseEncryptionException

Thrown when an encrypted database cannot be opened, keyed or converted.

This extends java.io.IOException deliberately: every database method already declares IOException, so existing catch blocks keep compiling and keep working. Code that wants to tell the failure modes apart can catch this type and switch on #getErrorCode().

Example

try {
    db = Database.openOrCreate("secure.db", DatabaseConfig.passphrase(entered));
} catch (DatabaseEncryptionException err) {
    if (err.getErrorCode() == DatabaseEncryptionException.WRONG_KEY) {
        showRetryPrompt();
    } else {
        throw err;
    }
}

Fields

public static final int NOT_SUPPORTED = 1The platform cannot open encrypted databases at all.
public static final int WRONG_KEY = 2The supplied passphrase or key does not decrypt this database, or the file is not a database at all.
public static final int KEY_UNAVAILABLE = 3A managed key was requested but the platform key store could not produce or persist one.
public static final int MIGRATION_FAILED = 4Converting a database between encrypted and plaintext form failed part way through.

Constructors

public DatabaseEncryptionException(int errorCode, String message)Creates an exception with the given code and message.
public DatabaseEncryptionException(int errorCode, String message, Throwable cause)Creates an exception with the given code, message and underlying cause.

Methods

public int getErrorCode()Returns the code identifying why the operation failed.

Inherited methods

Field details

NOT_SUPPORTED

public static final int NOT_SUPPORTED = 1

The platform cannot open encrypted databases at all. Check Database#isEncryptionSupported() before offering encryption in the UI.

A request for encryption on such a platform always fails with this code. It never silently falls back to an unencrypted database.

WRONG_KEY

public static final int WRONG_KEY = 2
The supplied passphrase or key does not decrypt this database, or the file is not a database at all. These two cases are indistinguishable by design: a correct cipher reveals nothing about a wrong key.

KEY_UNAVAILABLE

public static final int KEY_UNAVAILABLE = 3
A managed key was requested but the platform key store could not produce or persist one. The database is not opened, because opening it unencrypted would silently downgrade the protection the caller asked for.

MIGRATION_FAILED

public static final int MIGRATION_FAILED = 4
Converting a database between encrypted and plaintext form failed part way through. The original file is left untouched.

Constructor details

DatabaseEncryptionException

public DatabaseEncryptionException(int errorCode, String message)
Creates an exception with the given code and message.

Parameters

errorCode int
one of the constants declared by this class
message String
a human readable description of the failure

DatabaseEncryptionException

public DatabaseEncryptionException(int errorCode, String message, Throwable cause)
Creates an exception with the given code, message and underlying cause.

Parameters

errorCode int
one of the constants declared by this class
message String
a human readable description of the failure
cause Throwable
the underlying failure, retained for diagnostics

Method details

getErrorCode

public int getErrorCode()
Returns the code identifying why the operation failed.

Returns

one of #NOT_SUPPORTED, #WRONG_KEY, #KEY_UNAVAILABLE or #MIGRATION_FAILED