{
  "schema_version": 2,
  "id": "develop/clients/jedis/error-handling",
  "title": "Error handling",
  "url": "https://redis.io/docs/latest/develop/clients/jedis/error-handling/",
  "summary": "Learn how to handle errors when using Jedis.",
  "tags": [],
  "last_updated": "2026-09-08T15:41:24+01:00",
  "page_type": "content",
  "content_hash": "6d722a9564d88704beed33ee67d23b26c2b5fc10ad0e7478d268590d777d29db",
  "sections": [
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "Jedis uses **exceptions** to signal errors. Code examples in the documentation often omit error handling for brevity, but it is essential in production code. This page explains how Jedis's error handling works and how to apply common error handling patterns.\n\nFor an overview of error types and handling strategies, see [Error handling](https://redis.io/docs/latest/develop/clients/error-handling).\nSee also [Production usage](https://redis.io/docs/latest/develop/clients/jedis/produsage)\nfor more information on connection management, timeouts, and other aspects of\napp reliability."
    },
    {
      "id": "exception-hierarchy",
      "title": "Exception hierarchy",
      "role": "errors",
      "text": "Jedis organizes exceptions in a hierarchy rooted at `JedisException`, which extends `RuntimeException`. All Jedis exceptions are unchecked exceptions:\n\n[code example]"
    },
    {
      "id": "key-exceptions",
      "title": "Key exceptions",
      "role": "content",
      "text": "The following exceptions are the most commonly encountered in Jedis applications.\nSee [Categories of errors](https://redis.io/docs/latest/develop/clients/error-handling#categories-of-errors)\nfor a more detailed discussion of these errors and their causes.\n\n| Exception | When it occurs | Recoverable | Recommended action |\n|---|---|---|---|\n| `JedisConnectionException` | Connection lost or closed unexpectedly | ✅ | Retry with backoff or fall back |\n| `JedisAccessControlException` | Authentication failure or permission denied | ❌ | Fix credentials or permissions |\n| `JedisDataException` | Problem with data being sent or received | ❌ | Fix the data or command |\n| `JedisException` | Unexpected errors (catch-all) | ❌ | Log and investigate |"
    },
    {
      "id": "applying-error-handling-patterns",
      "title": "Applying error handling patterns",
      "role": "content",
      "text": "The [Error handling](https://redis.io/docs/latest/develop/clients/error-handling) overview\ndescribes four main patterns. The sections below show how to implement them in\nJedis:"
    },
    {
      "id": "pattern-1-fail-fast",
      "title": "Pattern 1: Fail fast",
      "role": "content",
      "text": "Catch specific exceptions that represent unrecoverable errors and re-throw them (see\n[Pattern 1: Fail fast](https://redis.io/docs/latest/develop/clients/error-handling#pattern-1-fail-fast)\nfor a full description):\n\n[code example]"
    },
    {
      "id": "pattern-2-graceful-degradation",
      "title": "Pattern 2: Graceful degradation",
      "role": "content",
      "text": "Catch specific errors and fall back to an alternative, where possible (see\n[Pattern 2: Graceful degradation](https://redis.io/docs/latest/develop/clients/error-handling#pattern-2-graceful-degradation)\nfor a full description):\n\n[code example]"
    },
    {
      "id": "pattern-3-retry-with-backoff",
      "title": "Pattern 3: Retry with backoff",
      "role": "content",
      "text": "Retry on temporary errors like connection failures (see\n[Pattern 3: Retry with backoff](https://redis.io/docs/latest/develop/clients/error-handling#pattern-3-retry-with-backoff)\nfor a full description):\n\n[code example]"
    },
    {
      "id": "pattern-4-log-and-continue",
      "title": "Pattern 4: Log and continue",
      "role": "content",
      "text": "Log non-critical errors and continue (see\n[Pattern 4: Log and continue](https://redis.io/docs/latest/develop/clients/error-handling#pattern-4-log-and-continue)\nfor a full description):\n\n[code example]"
    },
    {
      "id": "see-also",
      "title": "See also",
      "role": "related",
      "text": "- [Error handling](https://redis.io/docs/latest/develop/clients/error-handling)\n- [Production usage](https://redis.io/docs/latest/develop/clients/jedis/produsage)"
    }
  ],
  "examples": [
    {
      "id": "exception-hierarchy-ex0",
      "language": "hierarchy {type=\"exception\"}",
      "code": "\"JedisException\":\n    _meta:\n        description: \"Base class for all Jedis exceptions\"\n    \"JedisDataException\":\n        \"JedisRedirectionException\":\n            \"JedisMovedDataException\":\n            \"JedisAskDataException\":\n        \"AbortedTransactionException\":\n        \"JedisAccessControlException\":\n        \"JedisNoScriptException\":\n    \"JedisClusterException\":\n        \"JedisClusterOperationException\":\n        \"JedisConnectionException\":\n        \"JedisValidationException\":\n    \"InvalidURIException\":",
      "section_id": "exception-hierarchy"
    },
    {
      "id": "pattern-1-fail-fast-ex0",
      "language": "java",
      "code": "try (RedisClient jedis = RedisClient.create()) {\n    String result = jedis.get(key);\n} catch (JedisDataException e) {\n    // This indicates a bug in our code\n    throw e;\n}",
      "section_id": "pattern-1-fail-fast"
    },
    {
      "id": "pattern-2-graceful-degradation-ex0",
      "language": "java",
      "code": "try (RedisClient jedis = RedisClient.create()) {\n    String cachedValue = jedis.get(key);\n    if (cachedValue != null) {\n        return cachedValue;\n    }\n} catch (JedisConnectionException e) {\n    logger.warn(\"Cache unavailable, using database\");\n    return database.get(key);\n}\n\n// Fallback to database\nreturn database.get(key);",
      "section_id": "pattern-2-graceful-degradation"
    },
    {
      "id": "pattern-3-retry-with-backoff-ex0",
      "language": "java",
      "code": "int maxRetries = 3;\nint retryDelay = 100;\n\nfor (int attempt = 0; attempt < maxRetries; attempt++) {\n    try (RedisClient jedis = RedisClient.create()) {\n        return jedis.get(key);\n    } catch (JedisConnectionException e) {\n        if (attempt < maxRetries - 1) {\n            try {\n                Thread.sleep(retryDelay);\n                retryDelay *= 2;  // Exponential backoff\n            } catch (InterruptedException ie) {\n                Thread.currentThread().interrupt();\n                throw new RuntimeException(ie);\n            }\n        } else {\n            throw e;\n        }\n    }\n}",
      "section_id": "pattern-3-retry-with-backoff"
    },
    {
      "id": "pattern-4-log-and-continue-ex0",
      "language": "java",
      "code": "try (RedisClient jedis = RedisClient.create()) {\n    jedis.setex(key, 3600, value);\n} catch (JedisConnectionException e) {\n    logger.warn(\"Failed to cache \" + key + \", continuing without cache\");\n    // Application continues normally\n}",
      "section_id": "pattern-4-log-and-continue"
    }
  ]
}
