{
  "id": "java/negative-array-size",
  "signature": "java.lang.NegativeArraySizeException",
  "signature_zh": "java.lang.NegativeArraySizeException",
  "regex": "java\\.lang\\.NegativeArraySizeException",
  "domain": "java",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "Thrown when an array is created with a negative size, usually due to an integer overflow or a bug in array size calculation.",
  "root_cause_type": "generic",
  "root_cause_zh": "当使用负数大小创建数组时抛出，通常是由于整数溢出或数组大小计算错误。",
  "versions": [
    {
      "version": "Java 8",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Java 11",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Java 17",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Java 21",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "The root cause (negative size) is not fixed; retrying will throw the same exception.",
      "fail_rate": 0.95,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "This masks the underlying bug and may lead to incorrect program behavior or data loss.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "If the size is Integer.MIN_VALUE, Math.abs() returns a negative value due to overflow, still causing the exception.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Validate the array size before creation: `if (size < 0) throw new IllegalArgumentException(\"Size must be non-negative: \" + size);`.",
      "success_rate": 0.95,
      "how": "Validate the array size before creation: `if (size < 0) throw new IllegalArgumentException(\"Size must be non-negative: \" + size);`.",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use long arithmetic for size calculation to prevent integer overflow: `long safeSize = (long) a * b; if (safeSize > Integer.MAX_VALUE) throw new IllegalArgumentException(\"Size too large\");`.",
      "success_rate": 0.9,
      "how": "Use long arithmetic for size calculation to prevent integer overflow: `long safeSize = (long) a * b; if (safeSize > Integer.MAX_VALUE) throw new IllegalArgumentException(\"Size too large\");`.",
      "condition": "",
      "sources": []
    },
    {
      "action": "Add a guard clause: `int size = computeSize(); if (size < 0) { size = 0; }` to handle edge cases gracefully.",
      "success_rate": 0.85,
      "how": "Add a guard clause: `int size = computeSize(); if (size < 0) { size = 0; }` to handle edge cases gracefully.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "Validate the array size before creation: `if (size < 0) throw new IllegalArgumentException(\"Size must be non-negative: \" + size);`.",
    "Use long arithmetic for size calculation to prevent integer overflow: `long safeSize = (long) a * b; if (safeSize > Integer.MAX_VALUE) throw new IllegalArgumentException(\"Size too large\");`.",
    "Add a guard clause: `int size = computeSize(); if (size < 0) { size = 0; }` to handle edge cases gracefully."
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://docs.oracle.com/javase/8/docs/api/java/lang/NegativeArraySizeException.html",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.81,
  "fix_success_rate": 0.87,
  "resolvable": "true",
  "first_seen": "2024-06-12",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}