{"id":319,"date":"2025-06-29T15:19:22","date_gmt":"2025-06-29T15:19:22","guid":{"rendered":"https:\/\/thetestdata.com\/blog\/?p=319"},"modified":"2025-07-16T10:13:40","modified_gmt":"2025-07-16T10:13:40","slug":"explain-exceptions-hierarchy-in-java","status":"publish","type":"post","link":"https:\/\/thetestdata.com\/blog\/explain-exceptions-hierarchy-in-java\/","title":{"rendered":"Explain Exceptions hierarchy in java?"},"content":{"rendered":"\n<p>In Java, <strong>exceptions are organized in a class hierarchy<\/strong> rooted at the <code>Throwable<\/code> class. This structure helps developers understand which types of problems can be caught and handled, and which ones should be avoided or allowed to crash the program.<\/p>\n\n\n\n<p>\ud83c\udf33 Java Exception Hierarchy Overview<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>                Object\n                   |\n               Throwable\n               \/       \\\n           Error      Exception\n                        \/     \\\n         Checked Exceptions  Unchecked Exceptions (RuntimeException)<\/code><\/pre>\n\n\n\n<p>Object | Throwable \/ \\ Error Exception \/ \\ Checked Exceptions Unchecked Exceptions (RuntimeException)<\/p>\n\n\n\n<p>\ud83d\udd39 1. <code>Throwable<\/code> (Base Class)<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The superclass of <strong>all errors and exceptions<\/strong> in Java.<\/li>\n\n\n\n<li>Only objects that are instances of <code>Throwable<\/code> (or its subclasses) can be thrown or caught.<\/li>\n<\/ul>\n\n\n\n<p>\ud83d\udd38 2. <code>Error<\/code><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Represents <strong>serious problems<\/strong> that applications should not try to handle.<\/li>\n\n\n\n<li>Examples:<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>OutOfMemoryError<\/code><\/li>\n\n\n\n<li><code>StackOverflowError<\/code><\/li>\n\n\n\n<li><code>VirtualMachineError<\/code><\/li>\n<\/ul>\n\n\n\n<p>These are typically caused by the JVM or system environment and are <strong>not meant to be caught<\/strong> in normal code.<\/p>\n\n\n\n<p>\ud83d\udd38 3. <code>Exception<\/code><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Represents <strong>conditions that a program might want to catch<\/strong>.<\/li>\n\n\n\n<li>Divided into:<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Checked Exceptions<\/strong><\/li>\n\n\n\n<li><strong>Unchecked Exceptions (RuntimeException)<\/strong><\/li>\n<\/ul>\n\n\n\n<p>\u2705 Checked Exceptions<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Must be either <strong>caught<\/strong> or <strong>declared<\/strong> in the method signature using <code>throws<\/code>.<\/li>\n\n\n\n<li>Examples:<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>IOException<\/code><\/li>\n\n\n\n<li><code>SQLException<\/code><\/li>\n\n\n\n<li><code>FileNotFoundException<\/code><\/li>\n\n\n\n<li><code>ClassNotFoundException<\/code><\/li>\n<\/ul>\n\n\n\n<p>These are typically <strong>external issues<\/strong> (e.g., file not found, database error) that the program can recover from.<\/p>\n\n\n\n<p>\u26a0\ufe0f Unchecked Exceptions (RuntimeException)<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Do <strong>not need to be declared or caught<\/strong>.<\/li>\n\n\n\n<li>Occur due to <strong>programming errors<\/strong>.<\/li>\n\n\n\n<li>Examples:<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>NullPointerException<\/code><\/li>\n\n\n\n<li><code>ArrayIndexOutOfBoundsException<\/code><\/li>\n\n\n\n<li><code>IllegalArgumentException<\/code><\/li>\n\n\n\n<li><code>ArithmeticException<\/code><\/li>\n<\/ul>\n\n\n\n<p>These are often bugs in the code and should be fixed rather than caught.<\/p>\n\n\n\n<p>\ud83e\udde0 Summary Table<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Type<\/th><th>Subclass Of<\/th><th>Must Handle?<\/th><th>Examples<\/th><\/tr><\/thead><tbody><tr><td><code>Error<\/code><\/td><td><code>Throwable<\/code><\/td><td>\u274c No<\/td><td><code>OutOfMemoryError<\/code>, <code>StackOverflowError<\/code><\/td><\/tr><tr><td>Checked <code>Exception<\/code><\/td><td><code>Exception<\/code><\/td><td>\u2705 Yes<\/td><td><code>IOException<\/code>, <code>SQLException<\/code><\/td><\/tr><tr><td>Unchecked <code>Exception<\/code><\/td><td><code>RuntimeException<\/code><\/td><td>\u274c No<\/td><td><code>NullPointerException<\/code>, <code>ArithmeticException<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Understand the exception hierarchy in Java. Learn how Throwable, Exception, and Error classes structure handling of runtime, checked, and unchecked issues.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"site-container-style":"default","site-container-layout":"default","site-sidebar-layout":"default","disable-article-header":"default","disable-site-header":"default","disable-site-footer":"default","disable-content-area-spacing":"default","footnotes":""},"categories":[17],"tags":[],"class_list":["post-319","post","type-post","status-publish","format-standard","hentry","category-java-interview-questions"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts\/319","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/comments?post=319"}],"version-history":[{"count":2,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts\/319\/revisions"}],"predecessor-version":[{"id":429,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts\/319\/revisions\/429"}],"wp:attachment":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/media?parent=319"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/categories?post=319"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/tags?post=319"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}