{"id":11204,"date":"2014-12-02T12:01:44","date_gmt":"2014-12-02T12:01:44","guid":{"rendered":"https:\/\/www.whizlabs.com\/?p=11204"},"modified":"2020-09-01T07:18:20","modified_gmt":"2020-09-01T07:18:20","slug":"exception-handling-in-java","status":"publish","type":"post","link":"https:\/\/www.whizlabs.com\/blog\/exception-handling-in-java\/","title":{"rendered":"Exception Handling in Java"},"content":{"rendered":"<p>\u2018Exceptions\u2019, handling them, declaring them is yet another objective of the OCPJP exam.\u00a0 We will be dealing with \u2018Exceptions\u2019 and more particularly \u2018Handling Exceptions\u2019 in this post.<\/p>\n<p>According to the Oracle website, \u201cAn exception is an event, which occurs during the execution of a program that disrupts the normal flow of the program&#8217;s instructions.\u201d (Java Tutorials) When the normal flow is disrupted and an exception is thrown, the exception is handled by means of a \u201ctry-catch\u201d block.<\/p>\n<p><b>Types of exceptions:<br \/>\n<\/b>There are three types of exceptions \u2013 Checked exceptions, Errors and Runtime exceptions. This is the hierarchy of exceptions:<\/p>\n<p><a style=\"line-height: 1.5em\" href=\"https:\/\/www.whizlabs.com\/wp-content\/uploads\/2014\/12\/ExceptionHandling1.jpg\"><img decoding=\"async\" class=\"size-full wp-image-11206 alignnone\" alt=\"ExceptionHandling1\" src=\"https:\/\/www.whizlabs.com\/wp-content\/uploads\/2014\/12\/ExceptionHandling1.jpg\" width=\"606\" height=\"305\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><b style=\"line-height: 1.5em\">Checked exceptions:<br \/>\n<\/b>All exceptions are \u201cchecked exceptions\u201d except those under \u201cjava.lang.Error\u201d and \u201cjava.lang.RuntimeException\u201d and their sub-classes. One common example of a checked exception is when a user tries to open a file that is not present \u2013 an exception is thrown. Other examples of checked exceptions are hardware problems and common bugs. It is up to the programmer to handle these exceptions appropriately.<\/p>\n<p><b>Errors:<br \/>\n<\/b>Errors are <i>unchecked exceptions.<\/i> Errors are conditions that usually a compiler is not expected to recover from. \u00a0These conditions occur, external to the application.\u00a0 Hence, errors are not caught by the \u201ctry-catch\u201d constructs. Thread death, IO errors are some examples of \u2018Errors\u2019.<\/p>\n<p><b>RuntimeException:<br \/>\n<\/b>Runtime exceptions are again <i>unchecked exceptions. <\/i>They are similar to errors \u2013 in that the application is not expected to recover from Runtime exceptions. The only difference is that runtime exceptions occur within the application. Some examples of runtime exceptions are \u2018ArithmeticException\u2019, \u2018NullPointerException\u2019 etc. All sub-classes of the \u2018java.lang.RuntimeException\u2019 fall under this. It is not wrong to handle unchecked exceptions \u2013 however the compiler doesn\u2019t expect this.<\/p>\n<p><b>Handling exceptions:<br \/>\n<\/b>Having seen the different types of exceptions, we now see how an exception is handled. All exceptions are handled via the \u2018try-catch\u2019 block. The list below illustrates the syntax of a \u201ctry-catch\u201d block.<\/p>\n<p>try{<br \/>\n\/\/1. some code here that might raise an exception<br \/>\n}<\/p>\n<p>2. catch(specificException e){<br \/>\n\/\/ do appropriate action here<br \/>\n}<br \/>\ncatch(GeneralException e){<br \/>\n\/\/ do appropriate action here<br \/>\n}<br \/>\nfinally{<br \/>\n\/\/clean up code is here<br \/>\n}<\/p>\n<p>If the code in the \u201ctry\u201d block at line 1 produces a \u2018specificException\u2019, it will be propagated to line 2. The exception will be handled in the \u2018catch\u2019 block and the control will then move to the \u2018finally\u2019 block. Clean up code is executed in the \u201cfinally\u201d block and control moves out of the \u201ctry-catch\u201d structure.<br \/>\nEven if there are no exceptions at line 1, control will first move to the \u2018finally\u2019 block and then leave the \u201ctry-catch\u201d structure.<\/p>\n<p>Here are some key points regarding the \u2018finally\u2019 block:<\/p>\n<ol>\n<li>\u2018finally\u2019 block <i>almost<\/i> always runs.<br \/>\n<span style=\"line-height: 1.5em\">If an exception is thrown, it runs. If an exception is <\/span><i style=\"line-height: 1.5em\">not<\/i><span style=\"line-height: 1.5em\"> thrown, it runs. If an exception is caught, it runs. If an exception is not caught, it runs!\u00a0(Kathy Sierra)<\/span><\/li>\n<li>\u2018finally\u2019 clauses are <b>not <\/b>required.<\/li>\n<\/ol>\n<p>Next, we see how specific exceptions and general exceptions must be stated.<br \/>\nNow consider the following piece of code:<\/p>\n<p><a href=\"https:\/\/www.whizlabs.com\/wp-content\/uploads\/2014\/12\/ExceptionHandling2.jpg\"><img decoding=\"async\" class=\"alignnone size-full wp-image-11207\" alt=\"ExceptionHandling2\" src=\"https:\/\/www.whizlabs.com\/wp-content\/uploads\/2014\/12\/ExceptionHandling2.jpg\" width=\"619\" height=\"483\" \/><\/a><\/p>\n<p>Here, since we try to access the fourth element of the array, an \u2018ArrayIndexOutOfBoundsException\u2019 is thrown.\u00a0 Notice, that, the \u2018ArrayIndexOutOfBoundsException\u2019 is stated before \u2018IndexOutOfBoundsException\u2019. \u00a0<b>In other words, specific exceptions are declared before the general one.<br \/>\n<\/b><\/p>\n<p><b><\/b>If the code was modified and the \u201cIndexOutOfBoundsException\u201d was placed before the \u201cArrayIndexOutOfBoundsException\u201d, a compiler error would result.<\/p>\n<p><a href=\"https:\/\/www.whizlabs.com\/wp-content\/uploads\/2014\/12\/ExceptionHandling3.jpg\"><img decoding=\"async\" class=\"alignnone size-full wp-image-11208\" alt=\"ExceptionHandling3\" src=\"https:\/\/www.whizlabs.com\/wp-content\/uploads\/2014\/12\/ExceptionHandling3.jpg\" width=\"734\" height=\"542\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>We dealt with the different types of exceptions and ways of handling them in this post. We will continue our discussion of exceptions in the next post.<\/p>\n<address>Bibliography<\/address>\n<address><i>Java Tutorials<\/i>. (n.d.). Retrieved Nov 26, 2014, from Oracle Java Documentation: http:\/\/docs.oracle.com\/javase\/tutorial\/essential\/exceptions\/definition.html<\/address>\n<address>Sun Certified Programmer for Java 6 Study Guide. In B. B. Kathy Sierra.<\/address>\n<pre><\/pre>\n<p><span>Preparing for OCPJP 7 Certification? Pass in 1st attempt through <\/span><a href=\"https:\/\/www.whizlabs.com\/oracle-certified-professional-java-se-7-programmer\/\">Whizlabs OCPJP 7 Practice Test !<\/a> <span>\u00a0<\/span><span>Start with Free Trial!<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u2018Exceptions\u2019, handling them, declaring them is yet another objective of the OCPJP exam.\u00a0 We will be dealing with \u2018Exceptions\u2019 and more particularly \u2018Handling Exceptions\u2019 in this post. According to the Oracle website, \u201cAn exception is an event, which occurs during the execution of a program that disrupts the normal flow of the program&#8217;s instructions.\u201d (Java Tutorials) When the normal flow is disrupted and an exception is thrown, the exception is handled by means of a \u201ctry-catch\u201d block. Types of exceptions: There are three types of exceptions \u2013 Checked exceptions, Errors and Runtime exceptions. This is the hierarchy of exceptions: &nbsp; [&hellip;]<\/p>\n","protected":false},"author":220,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[13],"tags":[982],"class_list":["post-11204","post","type-post","status-publish","format-standard","hentry","category-java","tag-java-exception-handling"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"profile_24":false,"profile_48":false,"profile_96":false,"profile_150":false,"profile_300":false,"tptn_thumbnail":false,"web-stories-poster-portrait":false,"web-stories-publisher-logo":false,"web-stories-thumbnail":false},"uagb_author_info":{"display_name":"Aditi Malhotra","author_link":"https:\/\/www.whizlabs.com\/blog\/author\/aditi\/"},"uagb_comment_info":7,"uagb_excerpt":"\u2018Exceptions\u2019, handling them, declaring them is yet another objective of the OCPJP exam.\u00a0 We will be dealing with \u2018Exceptions\u2019 and more particularly \u2018Handling Exceptions\u2019 in this post. According to the Oracle website, \u201cAn exception is an event, which occurs during the execution of a program that disrupts the normal flow of the program&#8217;s instructions.\u201d (Java&hellip;","_links":{"self":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/11204","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/users\/220"}],"replies":[{"embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/comments?post=11204"}],"version-history":[{"count":1,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/11204\/revisions"}],"predecessor-version":[{"id":75997,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/11204\/revisions\/75997"}],"wp:attachment":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/media?parent=11204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/categories?post=11204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/tags?post=11204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}