[{"data":1,"prerenderedAt":956},["ShallowReactive",2],{"blog-\u002Fblog\u002Fjava-exception-handling-checked-unchecked":3},{"id":4,"title":5,"body":6,"description":941,"difficulty":942,"extension":943,"framework":944,"frameworkSlug":149,"meta":945,"navigation":946,"order":156,"path":947,"qaPath":948,"seo":949,"stem":950,"subtopic":951,"topic":952,"topicSlug":953,"updated":954,"__hash__":955},"blog\u002Fblog\u002Fjava-exception-handling-checked-unchecked.md","Java Exception Handling — Checked vs Unchecked, try\u002Fcatch & Best Practices",{"type":7,"value":8,"toc":929},"minimark",[9,14,18,22,33,84,94,102,106,145,204,216,220,248,349,371,374,381,450,479,483,500,577,583,587,600,660,671,675,700,759,765,769,816,888,892,925],[10,11,13],"h2",{"id":12},"java-exception-handling","Java exception handling",[15,16,17],"p",{},"Exceptions are how Java signals that something went wrong, separating error-handling code\nfrom the main logic. Handle them well and your programs fail safely and diagnosably;\nhandle them poorly — swallowing errors, catching too broadly, leaking resources — and you\nget silent corruption that's miserable to debug. This guide covers the hierarchy,\nchecked vs unchecked, the mechanics, and the practices that matter.",[10,19,21],{"id":20},"the-exception-hierarchy","The exception hierarchy",[15,23,24,25,32],{},"Everything throwable descends from ",[26,27,28],"strong",{},[29,30,31],"code",{},"Throwable",", which splits into:",[34,35,36,53],"ul",{},[37,38,39,44,45,48,49,52],"li",{},[26,40,41],{},[29,42,43],{},"Error"," — serious, usually unrecoverable JVM problems (",[29,46,47],{},"OutOfMemoryError",",\n",[29,50,51],{},"StackOverflowError","). Don't catch these.",[37,54,55,60,61],{},[26,56,57],{},[29,58,59],{},"Exception"," — application-level conditions you may handle.\n",[34,62,63,75],{},[37,64,65,70,71,74],{},[26,66,67],{},[29,68,69],{},"RuntimeException"," and subclasses are ",[26,72,73],{},"unchecked",".",[37,76,77,78,80,81,74],{},"All other ",[29,79,59],{}," subclasses are ",[26,82,83],{},"checked",[85,86,91],"pre",{"className":87,"code":89,"language":90},[88],"language-text","Throwable\n├── Error                 (unchecked — don't catch)\n└── Exception\n    ├── RuntimeException   (unchecked)\n    └── IOException, SQLException...  (checked)\n","text",[29,92,89],{"__ignoreMap":93},"",[15,95,96,97,99,100,74],{},"So \"checked vs unchecked\" is simply about whether the class sits under\n",[29,98,69],{},"\u002F",[29,101,43],{},[10,103,105],{"id":104},"checked-vs-unchecked","Checked vs unchecked",[34,107,108,129],{},[37,109,110,113,114,117,118,121,122,48,125,128],{},[26,111,112],{},"Checked"," exceptions are verified by the compiler: a method must either ",[29,115,116],{},"catch"," them\nor declare ",[29,119,120],{},"throws",". They represent recoverable, expected conditions (",[29,123,124],{},"IOException",[29,126,127],{},"SQLException",").",[37,130,131,134,135,138,139,138,142,128],{},[26,132,133],{},"Unchecked"," exceptions aren't compiler-enforced — usually programming bugs\n(",[29,136,137],{},"NullPointerException",", ",[29,140,141],{},"IllegalArgumentException",[29,143,144],{},"ArrayIndexOutOfBoundsException",[85,146,150],{"className":147,"code":148,"language":149,"meta":93,"style":93},"language-java shiki shiki-themes github-light github-dark","void read() throws IOException {     \u002F\u002F checked -> must declare or catch\n  Files.readString(Path.of(\"x\"));\n}\n","java",[29,151,152,173,198],{"__ignoreMap":93},[153,154,157,161,165,169],"span",{"class":155,"line":156},"line",1,[153,158,160],{"class":159},"szBVR","void",[153,162,164],{"class":163},"sScJk"," read",[153,166,168],{"class":167},"sVt8B","() throws IOException {     ",[153,170,172],{"class":171},"sJ8bj","\u002F\u002F checked -> must declare or catch\n",[153,174,176,179,182,185,188,191,195],{"class":155,"line":175},2,[153,177,178],{"class":167},"  Files.",[153,180,181],{"class":163},"readString",[153,183,184],{"class":167},"(Path.",[153,186,187],{"class":163},"of",[153,189,190],{"class":167},"(",[153,192,194],{"class":193},"sZZnC","\"x\"",[153,196,197],{"class":167},"));\n",[153,199,201],{"class":155,"line":200},3,[153,202,203],{"class":167},"}\n",[15,205,206,207,209,210,212,213,215],{},"The guideline: ",[26,208,83],{}," for conditions a caller can reasonably recover from,\n",[26,211,73],{}," for bugs and contract violations. Modern frameworks (Spring) lean heavily\ntoward unchecked to avoid ",[29,214,120],{}," clutter.",[10,217,219],{"id":218},"try-catch-finally","try, catch, finally",[15,221,222,225,226,228,229,232,233,236,237,240,241,243,244,247],{},[29,223,224],{},"try"," wraps risky code, ",[29,227,116],{}," handles specific types, and ",[29,230,231],{},"finally"," runs ",[26,234,235],{},"always"," —\neven after a ",[29,238,239],{},"return"," — for cleanup. Order ",[29,242,116],{}," blocks ",[26,245,246],{},"specific-before-general",", or\nthe compiler complains.",[85,249,251],{"className":147,"code":250,"language":149,"meta":93,"style":93},"try {\n  read();\n} catch (FileNotFoundException e) { \u002F\u002F specific first\n  recoverMissing();\n} catch (IOException e) {            \u002F\u002F broader after\n  log(e);\n} finally {\n  cleanup();                         \u002F\u002F always runs\n}\n",[29,252,253,260,268,288,296,314,323,332,344],{"__ignoreMap":93},[153,254,255,257],{"class":155,"line":156},[153,256,224],{"class":159},[153,258,259],{"class":167}," {\n",[153,261,262,265],{"class":155,"line":175},[153,263,264],{"class":163},"  read",[153,266,267],{"class":167},"();\n",[153,269,270,273,275,278,282,285],{"class":155,"line":200},[153,271,272],{"class":167},"} ",[153,274,116],{"class":159},[153,276,277],{"class":167}," (FileNotFoundException ",[153,279,281],{"class":280},"s4XuR","e",[153,283,284],{"class":167},") { ",[153,286,287],{"class":171},"\u002F\u002F specific first\n",[153,289,291,294],{"class":155,"line":290},4,[153,292,293],{"class":163},"  recoverMissing",[153,295,267],{"class":167},[153,297,299,301,303,306,308,311],{"class":155,"line":298},5,[153,300,272],{"class":167},[153,302,116],{"class":159},[153,304,305],{"class":167}," (IOException ",[153,307,281],{"class":280},[153,309,310],{"class":167},") {            ",[153,312,313],{"class":171},"\u002F\u002F broader after\n",[153,315,317,320],{"class":155,"line":316},6,[153,318,319],{"class":163},"  log",[153,321,322],{"class":167},"(e);\n",[153,324,326,328,330],{"class":155,"line":325},7,[153,327,272],{"class":167},[153,329,231],{"class":159},[153,331,259],{"class":167},[153,333,335,338,341],{"class":155,"line":334},8,[153,336,337],{"class":163},"  cleanup",[153,339,340],{"class":167},"();                         ",[153,342,343],{"class":171},"\u002F\u002F always runs\n",[153,345,347],{"class":155,"line":346},9,[153,348,203],{"class":167},[15,350,351,353,354,357,358,361,362,99,364,367,368,370],{},[29,352,231],{}," runs in virtually all cases — only ",[29,355,356],{},"System.exit()",", a JVM crash, or an infinite\nloop skip it. ",[26,359,360],{},"Never"," put a ",[29,363,239],{},[29,365,366],{},"throw"," in ",[29,369,231],{},": it overrides the try's result\nand silently swallows pending exceptions — a notorious bug.",[10,372,373],{"id":373},"try-with-resources",[15,375,376,377,380],{},"For anything that needs closing (streams, connections, locks), declare it in a\ntry-with-resources; it auto-closes in ",[26,378,379],{},"reverse order"," when the block exits, normally or\nvia exception.",[85,382,384],{"className":147,"code":383,"language":149,"meta":93,"style":93},"try (var br = Files.newBufferedReader(path);\n     var conn = dataSource.getConnection()) {\n  return br.readLine();\n} \u002F\u002F br and conn closed automatically\n",[29,385,386,411,430,443],{"__ignoreMap":93},[153,387,388,390,393,396,399,402,405,408],{"class":155,"line":156},[153,389,224],{"class":159},[153,391,392],{"class":167}," (",[153,394,395],{"class":159},"var",[153,397,398],{"class":167}," br ",[153,400,401],{"class":159},"=",[153,403,404],{"class":167}," Files.",[153,406,407],{"class":163},"newBufferedReader",[153,409,410],{"class":167},"(path);\n",[153,412,413,416,419,421,424,427],{"class":155,"line":175},[153,414,415],{"class":159},"     var",[153,417,418],{"class":167}," conn ",[153,420,401],{"class":159},[153,422,423],{"class":167}," dataSource.",[153,425,426],{"class":163},"getConnection",[153,428,429],{"class":167},"()) {\n",[153,431,432,435,438,441],{"class":155,"line":200},[153,433,434],{"class":159},"  return",[153,436,437],{"class":167}," br.",[153,439,440],{"class":163},"readLine",[153,442,267],{"class":167},[153,444,445,447],{"class":155,"line":290},[153,446,272],{"class":167},[153,448,449],{"class":171},"\u002F\u002F br and conn closed automatically\n",[15,451,452,453,456,457,459,460,463,464,467,468,471,472,392,475,478],{},"The resource must implement ",[29,454,455],{},"AutoCloseable",". This replaces error-prone manual ",[29,458,231],{},"\ncleanup and correctly handles ",[26,461,462],{},"suppressed exceptions",": if both the body and ",[29,465,466],{},"close()","\nthrow, the body's exception is primary and the close exception is attached via\n",[29,469,470],{},"getSuppressed()"," — nothing is lost. ",[26,473,474],{},"Multi-catch",[29,476,477],{},"catch (IOException | SQLException e)",") collapses identical handlers.",[10,480,482],{"id":481},"throw-vs-throws-and-custom-exceptions","throw vs throws, and custom exceptions",[15,484,485,487,488,490,491,493,494,496,497,74],{},[29,486,366],{}," is a statement that raises an exception now; ",[29,489,120],{}," is a method clause declaring\nwhich checked exceptions may propagate. Create custom exceptions by extending ",[29,492,59],{},"\n(checked) or ",[29,495,69],{}," (unchecked), passing the message and cause to ",[29,498,499],{},"super",[85,501,503],{"className":147,"code":502,"language":149,"meta":93,"style":93},"public class InsufficientFundsException extends RuntimeException {\n  public InsufficientFundsException(int shortfall) {\n    super(\"Short by \" + shortfall + \" cents\");\n  }\n}\n",[29,504,505,524,542,568,573],{"__ignoreMap":93},[153,506,507,510,513,516,519,522],{"class":155,"line":156},[153,508,509],{"class":159},"public",[153,511,512],{"class":159}," class",[153,514,515],{"class":163}," InsufficientFundsException",[153,517,518],{"class":159}," extends",[153,520,521],{"class":163}," RuntimeException",[153,523,259],{"class":167},[153,525,526,529,531,533,536,539],{"class":155,"line":175},[153,527,528],{"class":159},"  public",[153,530,515],{"class":163},[153,532,190],{"class":167},[153,534,535],{"class":159},"int",[153,537,538],{"class":280}," shortfall",[153,540,541],{"class":167},") {\n",[153,543,544,548,550,553,556,559,562,565],{"class":155,"line":200},[153,545,547],{"class":546},"sj4cs","    super",[153,549,190],{"class":167},[153,551,552],{"class":193},"\"Short by \"",[153,554,555],{"class":159}," +",[153,557,558],{"class":167}," shortfall ",[153,560,561],{"class":159},"+",[153,563,564],{"class":193}," \" cents\"",[153,566,567],{"class":167},");\n",[153,569,570],{"class":155,"line":290},[153,571,572],{"class":167},"  }\n",[153,574,575],{"class":155,"line":298},[153,576,203],{"class":167},[15,578,579,580,582],{},"Decide checked vs unchecked by whether the caller can recover. Custom exceptions let you\ncarry domain data and let callers ",[29,581,116],{}," precisely.",[10,584,586],{"id":585},"exception-chaining-and-translation","Exception chaining and translation",[15,588,589,590,593,594,597,598,128],{},"When you catch a low-level exception and rethrow a higher-level one, ",[26,591,592],{},"preserve the\noriginal as the cause"," so the full diagnostic trail survives. ",[26,595,596],{},"Exception translation","\nconverts low-level exceptions into ones appropriate to the current layer (a service\nshouldn't leak ",[29,599,127],{},[85,601,603],{"className":147,"code":602,"language":149,"meta":93,"style":93},"try {\n  jdbc.query();\n} catch (SQLException e) {\n  throw new DataAccessException(\"load failed\", e); \u002F\u002F e becomes the cause\n}\n",[29,604,605,611,621,634,656],{"__ignoreMap":93},[153,606,607,609],{"class":155,"line":156},[153,608,224],{"class":159},[153,610,259],{"class":167},[153,612,613,616,619],{"class":155,"line":175},[153,614,615],{"class":167},"  jdbc.",[153,617,618],{"class":163},"query",[153,620,267],{"class":167},[153,622,623,625,627,630,632],{"class":155,"line":200},[153,624,272],{"class":167},[153,626,116],{"class":159},[153,628,629],{"class":167}," (SQLException ",[153,631,281],{"class":280},[153,633,541],{"class":167},[153,635,636,639,642,645,647,650,653],{"class":155,"line":290},[153,637,638],{"class":159},"  throw",[153,640,641],{"class":159}," new",[153,643,644],{"class":163}," DataAccessException",[153,646,190],{"class":167},[153,648,649],{"class":193},"\"load failed\"",[153,651,652],{"class":167},", e); ",[153,654,655],{"class":171},"\u002F\u002F e becomes the cause\n",[153,657,658],{"class":155,"line":298},[153,659,203],{"class":167},[15,661,662,663,666,667,670],{},"The stack trace then shows ",[29,664,665],{},"Caused by:",". This is exactly what Spring's\n",[29,668,669],{},"DataAccessException"," hierarchy does.",[10,672,674],{"id":673},"nullpointerexception-and-optional","NullPointerException and Optional",[15,676,677,678,681,682,685,686,689,690,693,694,699],{},"An NPE happens when you dereference ",[29,679,680],{},"null"," — call a method, access a field, index an\narray, or unbox a null wrapper. Java 14+ gives ",[26,683,684],{},"helpful messages"," naming the exact null\nexpression. Prevent NPEs with ",[29,687,688],{},"Objects.requireNonNull",", constants-on-the-left\n(",[29,691,692],{},"\"x\".equals(s)","), and ",[26,695,696],{},[29,697,698],{},"Optional"," for maybe-absent return values:",[85,701,703],{"className":147,"code":702,"language":149,"meta":93,"style":93},"Optional\u003CUser> user = repo.findById(id);\nString name = user.map(User::name).orElse(\"unknown\");\n",[29,704,705,727],{"__ignoreMap":93},[153,706,707,710,713,716,718,721,724],{"class":155,"line":156},[153,708,709],{"class":167},"Optional\u003C",[153,711,712],{"class":159},"User",[153,714,715],{"class":167},"> user ",[153,717,401],{"class":159},[153,719,720],{"class":167}," repo.",[153,722,723],{"class":163},"findById",[153,725,726],{"class":167},"(id);\n",[153,728,729,732,734,737,740,743,746,749,752,754,757],{"class":155,"line":175},[153,730,731],{"class":167},"String name ",[153,733,401],{"class":159},[153,735,736],{"class":167}," user.",[153,738,739],{"class":163},"map",[153,741,742],{"class":167},"(User",[153,744,745],{"class":159},"::",[153,747,748],{"class":167},"name).",[153,750,751],{"class":163},"orElse",[153,753,190],{"class":167},[153,755,756],{"class":193},"\"unknown\"",[153,758,567],{"class":167},[15,760,761,762,764],{},"Use ",[29,763,698],{}," as a return type — not for fields, parameters, or collections (return an\nempty collection instead).",[10,766,768],{"id":767},"best-practices","Best practices",[34,770,771,785,794,800,806],{},[37,772,773,776,777,99,779,781,782,784],{},[26,774,775],{},"Catch specific"," exceptions, not bare ",[29,778,59],{},[29,780,31],{}," (which hides bugs and\ncatches ",[29,783,43],{},"s you can't handle).",[37,786,787,790,791,793],{},[26,788,789],{},"Never swallow"," — an empty ",[29,792,116],{}," hides failures and loses diagnostics. Handle, log,\nor rethrow.",[37,795,796,799],{},[26,797,798],{},"Throw early, catch late"," — validate inputs at the boundary; handle where you have\ncontext to recover.",[37,801,802,805],{},[26,803,804],{},"Don't use exceptions for control flow"," — building the stack trace is expensive.",[37,807,808,811,812,815],{},[26,809,810],{},"Preserve the cause"," when wrapping, and ",[26,813,814],{},"log once"," at the boundary, not at every\nlayer.",[85,817,819],{"className":147,"code":818,"language":149,"meta":93,"style":93},"\u002F\u002F swallowed — silent failure\ntry { risky(); } catch (Exception e) {}\n\u002F\u002F at minimum, log with the stack trace\ntry { risky(); } catch (Exception e) { log.error(\"risky failed\", e); throw e; }\n",[29,820,821,826,849,854],{"__ignoreMap":93},[153,822,823],{"class":155,"line":156},[153,824,825],{"class":171},"\u002F\u002F swallowed — silent failure\n",[153,827,828,830,833,836,839,841,844,846],{"class":155,"line":175},[153,829,224],{"class":159},[153,831,832],{"class":167}," { ",[153,834,835],{"class":163},"risky",[153,837,838],{"class":167},"(); } ",[153,840,116],{"class":159},[153,842,843],{"class":167}," (Exception ",[153,845,281],{"class":280},[153,847,848],{"class":167},") {}\n",[153,850,851],{"class":155,"line":200},[153,852,853],{"class":171},"\u002F\u002F at minimum, log with the stack trace\n",[153,855,856,858,860,862,864,866,868,870,873,876,878,881,883,885],{"class":155,"line":290},[153,857,224],{"class":159},[153,859,832],{"class":167},[153,861,835],{"class":163},[153,863,838],{"class":167},[153,865,116],{"class":159},[153,867,843],{"class":167},[153,869,281],{"class":280},[153,871,872],{"class":167},") { log.",[153,874,875],{"class":163},"error",[153,877,190],{"class":167},[153,879,880],{"class":193},"\"risky failed\"",[153,882,652],{"class":167},[153,884,366],{"class":159},[153,886,887],{"class":167}," e; }\n",[10,889,891],{"id":890},"recap","Recap",[15,893,894,895,392,899,901,902,904,905,907,908,910,911,914,915,917,918,921,922,924],{},"Java exceptions descend from ",[26,896,897],{},[29,898,31],{},[29,900,43],{}," vs ",[29,903,59],{},", the latter split\ninto ",[26,906,83],{}," and ",[26,909,73],{},"). Use ",[29,912,913],{},"try\u002Fcatch\u002Ffinally"," with specific-first ordering,\n",[26,916,373],{}," for automatic cleanup and suppressed-exception handling, and\n",[26,919,920],{},"chaining\u002Ftranslation"," to preserve causes across layers. Reserve checked exceptions for\nrecoverable conditions, lean on ",[29,923,698],{}," to dodge NPEs, and follow the practices — catch\nnarrowly, never swallow, throw early\u002Fcatch late, log once. Done right, exception handling\nmakes failures safe and debuggable instead of mysterious.",[926,927,928],"style",{},"html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .s4XuR, html code.shiki .s4XuR{--shiki-default:#E36209;--shiki-dark:#FFAB70}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":93,"searchDepth":175,"depth":175,"links":930},[931,932,933,934,935,936,937,938,939,940],{"id":12,"depth":175,"text":13},{"id":20,"depth":175,"text":21},{"id":104,"depth":175,"text":105},{"id":218,"depth":175,"text":219},{"id":373,"depth":175,"text":373},{"id":481,"depth":175,"text":482},{"id":585,"depth":175,"text":586},{"id":673,"depth":175,"text":674},{"id":767,"depth":175,"text":768},{"id":890,"depth":175,"text":891},"Java exception handling interview questions — checked vs unchecked, the exception hierarchy, try\u002Fcatch\u002Ffinally, try-with-resources, custom exceptions, chaining, and common pitfalls like swallowing exceptions.","medium","md","Java",{},true,"\u002Fblog\u002Fjava-exception-handling-checked-unchecked","\u002Fjava\u002Fexceptions\u002Fexception-handling",{"title":5,"description":941},"blog\u002Fjava-exception-handling-checked-unchecked","Exception Handling","Exceptions","exceptions","2026-06-18","5zYx8U77dEmOK8o07xP2elYyrpAFizYVk1aR3IDahz4",1781808673081]