{"id":113,"date":"2020-11-13T17:57:10","date_gmt":"2020-11-13T17:57:10","guid":{"rendered":"https:\/\/codeflarelimited.com\/blog\/?p=113"},"modified":"2021-05-31T05:45:38","modified_gmt":"2021-05-31T04:45:38","slug":"exceptions-in-java","status":"publish","type":"post","link":"https:\/\/codeflarelimited.com\/blog\/exceptions-in-java\/","title":{"rendered":"Exceptions in Java"},"content":{"rendered":"\n<p>An exception is a problem that arises during the execution of a program. <\/p>\n\n\n\n<p>When an exception occurs, the normal flow of the program is disrupted and the program or application terminates abruptly.<\/p>\n\n\n\n<p>An exception can occur for several reasons, some of which include:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>A user has entered an invalid data<\/li><li>A requested resource was not found<\/li><li>Network failure<\/li><\/ol>\n\n\n\n<p>Some of these exceptions can be caused by user error, others programmer&#8217;s error, or missing requested resource.<\/p>\n\n\n\n<p><strong><span class=\"has-inline-color has-accent-color\">Three (3) Categories of Exceptions<\/span><\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>Checked Exceptions:<\/strong><\/li><\/ol>\n\n\n\n<p>A checked exception is one that occurs at compile time. These are also called compile-time exceptions. These exceptions cannot just be ignored at the time of compilation, the programmer has to take care of it.<\/p>\n\n\n\n<p>2. <strong>Unchecked Exceptions<\/strong><\/p>\n\n\n\n<p>An unchecked exception is an exception that occurs at the time of execution. These are also called <strong>Run-time Exceptions<\/strong>. An example of this is programming bugs, such as logic errors. Runtime exceptions are ignored at the time of compilation.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">public class Unchecked_Exception {\n \n public static void main(String args[]){\n  int num[] = {1,2,3,4}\n   System.out.println(num[5]) \n\n\/\/Result: ArrayIndexOutOfBoundsException<\/code><\/pre>\n\n\n\n<p>3.<strong> Errors<\/strong><\/p>\n\n\n\n<p>These are not categorically exceptions but problems that arise beyond the control of the user of the program. Errors are typically ignored because there is very little the user can do to control it. They are also ignored at the time of compilation.<\/p>\n\n\n\n<p><strong>Built-in Exceptions<\/strong><\/p>\n\n\n\n<p>Java defines several exceptional classes inside the standard <strong>java.lang <\/strong>package. Since java.lang is implicitly imported into all Java programs, most exceptions derived from the Runtime Exception are automatically available.<\/p>\n\n\n\n<p><strong>Catching Exceptions<\/strong><\/p>\n\n\n\n<p>A method catches an exception using a combination of the <strong>try\/catch<\/strong> keywords. <\/p>\n\n\n\n<p>A try\/catch block is placed around the code that might generate an exception. Code within a try\/catch block is referred to as a protected code. It has the following syntax:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">try{\n\/\/Protected code\n}catch(ExceptionReference e){\n\/\/catch block\n}<\/code><\/pre>\n\n\n\n<p>The error variable <strong>e<\/strong> throws more light on what type of exception might have occurred.<\/p>\n\n\n\n<p>A catch statement involves declaring the type of exception you are trying to catch.<\/p>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">import java.io.*\npublic class ExceptionClass{\n \npublic static void main(String args[]){\n try{\n  int num[] = {1,2,3,4};\n  System.out.println(num[6]);\n  }catch(ArrayIndexOutOfBoundsException e){\n   System.out.println(\"An error occured: \" + e);\n  }\n}\n}\n  <\/code><\/pre>\n\n\n\n<p><a href=\"https:\/\/codeflarelimited.com\/catalouge\">See programming paths that you can take<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>An exception is a problem that arises during the execution of a program. When an exception occurs, the<\/p>\n","protected":false},"author":1,"featured_media":114,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[25],"tags":[26,27,8],"class_list":["post-113","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-exception","tag-java","tag-programming"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Exceptions in Java<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/codeflarelimited.com\/blog\/exceptions-in-java\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Exceptions in Java\" \/>\n<meta property=\"og:description\" content=\"An exception is a problem that arises during the execution of a program. When an exception occurs, the\" \/>\n<meta property=\"og:url\" content=\"https:\/\/codeflarelimited.com\/blog\/exceptions-in-java\/\" \/>\n<meta property=\"article:author\" content=\"https:\/\/facebook.com\/codeflretech\" \/>\n<meta property=\"article:published_time\" content=\"2020-11-13T17:57:10+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-05-31T04:45:38+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/codeflarelimited.com\/blog\/wp-content\/uploads\/2020\/11\/java.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"860\" \/>\n\t<meta property=\"og:image:height\" content=\"520\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"codeflare\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@codeflaretech\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"TechArticle\",\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/exceptions-in-java\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/exceptions-in-java\\\/\"},\"author\":{\"name\":\"codeflare\",\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/#\\\/schema\\\/person\\\/7e65653d49add95629f8c1053c5cd76a\"},\"headline\":\"Exceptions in Java\",\"datePublished\":\"2020-11-13T17:57:10+00:00\",\"dateModified\":\"2021-05-31T04:45:38+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/exceptions-in-java\\\/\"},\"wordCount\":322,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/exceptions-in-java\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/11\\\/java.jpg\",\"keywords\":[\"exception\",\"java\",\"programming\"],\"articleSection\":[\"java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/exceptions-in-java\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/exceptions-in-java\\\/\",\"url\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/exceptions-in-java\\\/\",\"name\":\"Exceptions in Java\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/exceptions-in-java\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/exceptions-in-java\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/11\\\/java.jpg\",\"datePublished\":\"2020-11-13T17:57:10+00:00\",\"dateModified\":\"2021-05-31T04:45:38+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/exceptions-in-java\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/exceptions-in-java\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/exceptions-in-java\\\/#primaryimage\",\"url\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/11\\\/java.jpg\",\"contentUrl\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/11\\\/java.jpg\",\"width\":860,\"height\":520,\"caption\":\"Handling exceptions in Java\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/exceptions-in-java\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"java\",\"item\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/java\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Exceptions in Java\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/\",\"name\":\"\",\"description\":\"Sustainable solutions\",\"publisher\":{\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/#organization\",\"name\":\"Codeflare Limited\",\"url\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/11\\\/codeflare.png\",\"contentUrl\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/11\\\/codeflare.png\",\"width\":1040,\"height\":263,\"caption\":\"Codeflare Limited\"},\"image\":{\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/#\\\/schema\\\/person\\\/7e65653d49add95629f8c1053c5cd76a\",\"name\":\"codeflare\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/59cef917c86d965eea581d2747f51bd6382003a68bfce7c8a4dfec98b4cd838d?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/59cef917c86d965eea581d2747f51bd6382003a68bfce7c8a4dfec98b4cd838d?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/59cef917c86d965eea581d2747f51bd6382003a68bfce7c8a4dfec98b4cd838d?s=96&d=mm&r=g\",\"caption\":\"codeflare\"},\"description\":\"Latest tech news and coding tips.\",\"sameAs\":[\"https:\\\/\\\/codeflarelimited.com\\\/blog\",\"https:\\\/\\\/facebook.com\\\/codeflretech\",\"https:\\\/\\\/instagram.com\\\/codeflaretech\",\"https:\\\/\\\/x.com\\\/codeflaretech\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCuBLtiYqsajHdqw0uyt7Ofw?sub_confirmation=1\"],\"url\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/author\\\/watcher\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Exceptions in Java","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/codeflarelimited.com\/blog\/exceptions-in-java\/","og_locale":"en_US","og_type":"article","og_title":"Exceptions in Java","og_description":"An exception is a problem that arises during the execution of a program. When an exception occurs, the","og_url":"https:\/\/codeflarelimited.com\/blog\/exceptions-in-java\/","article_author":"https:\/\/facebook.com\/codeflretech","article_published_time":"2020-11-13T17:57:10+00:00","article_modified_time":"2021-05-31T04:45:38+00:00","og_image":[{"width":860,"height":520,"url":"https:\/\/codeflarelimited.com\/blog\/wp-content\/uploads\/2020\/11\/java.jpg","type":"image\/jpeg"}],"author":"codeflare","twitter_card":"summary_large_image","twitter_creator":"@codeflaretech","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"TechArticle","@id":"https:\/\/codeflarelimited.com\/blog\/exceptions-in-java\/#article","isPartOf":{"@id":"https:\/\/codeflarelimited.com\/blog\/exceptions-in-java\/"},"author":{"name":"codeflare","@id":"https:\/\/codeflarelimited.com\/blog\/#\/schema\/person\/7e65653d49add95629f8c1053c5cd76a"},"headline":"Exceptions in Java","datePublished":"2020-11-13T17:57:10+00:00","dateModified":"2021-05-31T04:45:38+00:00","mainEntityOfPage":{"@id":"https:\/\/codeflarelimited.com\/blog\/exceptions-in-java\/"},"wordCount":322,"commentCount":0,"publisher":{"@id":"https:\/\/codeflarelimited.com\/blog\/#organization"},"image":{"@id":"https:\/\/codeflarelimited.com\/blog\/exceptions-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/codeflarelimited.com\/blog\/wp-content\/uploads\/2020\/11\/java.jpg","keywords":["exception","java","programming"],"articleSection":["java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/codeflarelimited.com\/blog\/exceptions-in-java\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/codeflarelimited.com\/blog\/exceptions-in-java\/","url":"https:\/\/codeflarelimited.com\/blog\/exceptions-in-java\/","name":"Exceptions in Java","isPartOf":{"@id":"https:\/\/codeflarelimited.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/codeflarelimited.com\/blog\/exceptions-in-java\/#primaryimage"},"image":{"@id":"https:\/\/codeflarelimited.com\/blog\/exceptions-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/codeflarelimited.com\/blog\/wp-content\/uploads\/2020\/11\/java.jpg","datePublished":"2020-11-13T17:57:10+00:00","dateModified":"2021-05-31T04:45:38+00:00","breadcrumb":{"@id":"https:\/\/codeflarelimited.com\/blog\/exceptions-in-java\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/codeflarelimited.com\/blog\/exceptions-in-java\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codeflarelimited.com\/blog\/exceptions-in-java\/#primaryimage","url":"https:\/\/codeflarelimited.com\/blog\/wp-content\/uploads\/2020\/11\/java.jpg","contentUrl":"https:\/\/codeflarelimited.com\/blog\/wp-content\/uploads\/2020\/11\/java.jpg","width":860,"height":520,"caption":"Handling exceptions in Java"},{"@type":"BreadcrumbList","@id":"https:\/\/codeflarelimited.com\/blog\/exceptions-in-java\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/codeflarelimited.com\/blog\/"},{"@type":"ListItem","position":2,"name":"java","item":"https:\/\/codeflarelimited.com\/blog\/java\/"},{"@type":"ListItem","position":3,"name":"Exceptions in Java"}]},{"@type":"WebSite","@id":"https:\/\/codeflarelimited.com\/blog\/#website","url":"https:\/\/codeflarelimited.com\/blog\/","name":"","description":"Sustainable solutions","publisher":{"@id":"https:\/\/codeflarelimited.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/codeflarelimited.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/codeflarelimited.com\/blog\/#organization","name":"Codeflare Limited","url":"https:\/\/codeflarelimited.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codeflarelimited.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/codeflarelimited.com\/blog\/wp-content\/uploads\/2020\/11\/codeflare.png","contentUrl":"https:\/\/codeflarelimited.com\/blog\/wp-content\/uploads\/2020\/11\/codeflare.png","width":1040,"height":263,"caption":"Codeflare Limited"},"image":{"@id":"https:\/\/codeflarelimited.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/codeflarelimited.com\/blog\/#\/schema\/person\/7e65653d49add95629f8c1053c5cd76a","name":"codeflare","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/59cef917c86d965eea581d2747f51bd6382003a68bfce7c8a4dfec98b4cd838d?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/59cef917c86d965eea581d2747f51bd6382003a68bfce7c8a4dfec98b4cd838d?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/59cef917c86d965eea581d2747f51bd6382003a68bfce7c8a4dfec98b4cd838d?s=96&d=mm&r=g","caption":"codeflare"},"description":"Latest tech news and coding tips.","sameAs":["https:\/\/codeflarelimited.com\/blog","https:\/\/facebook.com\/codeflretech","https:\/\/instagram.com\/codeflaretech","https:\/\/x.com\/codeflaretech","https:\/\/www.youtube.com\/channel\/UCuBLtiYqsajHdqw0uyt7Ofw?sub_confirmation=1"],"url":"https:\/\/codeflarelimited.com\/blog\/author\/watcher\/"}]}},"jetpack_featured_media_url":"https:\/\/codeflarelimited.com\/blog\/wp-content\/uploads\/2020\/11\/java.jpg","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/posts\/113","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/comments?post=113"}],"version-history":[{"count":3,"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/posts\/113\/revisions"}],"predecessor-version":[{"id":671,"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/posts\/113\/revisions\/671"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/media\/114"}],"wp:attachment":[{"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/media?parent=113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/categories?post=113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/tags?post=113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}