{"id":11807,"date":"2015-01-09T10:00:51","date_gmt":"2015-01-09T10:00:51","guid":{"rendered":"https:\/\/www.whizlabs.com\/?p=11807"},"modified":"2020-09-01T07:10:48","modified_gmt":"2020-09-01T07:10:48","slug":"exceptions-ii","status":"publish","type":"post","link":"https:\/\/www.whizlabs.com\/blog\/exceptions-ii\/","title":{"rendered":"Exceptions II"},"content":{"rendered":"<p>We will extend our discussion about \u2018Exceptions\u2019 which we had started in an earlier post. These were the key takeaways from our earlier post:<\/p>\n<ol>\n<li>Differences between checked and unchecked exceptions<\/li>\n<li>Important points regarding \u2018finally\u2019 clause<\/li>\n<li>Specific exceptions must be declared before the general exceptions<\/li>\n<\/ol>\n<p>In this post, we will discuss the legal and illegal forms of \u2018try-catch\u2019 constructs and more ways to declare exceptions.<\/p>\n<p>A. \u00a0Legal and Illegal forms of \u2018try-catch\u2019 constructs :<\/p>\n<p style=\"padding-left: 30px\">It is illegal to use \u2018try-catch-finally\u2019 clause without the \u2018catch\u2019 clause or the \u2018finally\u2019 clause. <b>In other words, it is illegal to use the \u2018try\u2019 clause alone.<\/b> When the \u2018try\u2019 clause is used alone, it will result in compiler error. Let us see an example to illustrate this:<\/p>\n<pre style=\"padding-left: 30px\">package whizlabs;\n public class except_2 \n {\n   public static void main(String[] args) \n {\n   int i=10,j; \/\/ TODO Auto-generated method stub\n   try\n   {\n    j = i\/10;\n   }\n  }\n }<\/pre>\n<p style=\"padding-left: 30px\">In the above example, the \u2018try\u2019 clause is used alone without the \u2018catch\u2019 clause or the \u2018finally\u2019 clause. This results in a compiler error.<\/p>\n<p>B.\u00a0<b>The \u2018catch\u2019 clause or the \u2018finally\u2019clause when used &#8211; must be used immediately after the \u2018try\u2019 clause.<\/b> It is illegal to use it after several statements following the \u2018try\u2019 clause. The following program illustrates this:<\/p>\n<pre style=\"padding-left: 30px\">package whizlabs;\n  public class except_2 {\n    public static void main(String[] args) {\n      int i=10,j; \/\/ TODO Auto-generated method stub\nA:    try{\n         j = i\/0;\n       }\n      System.out.println(\"Division by zero causes an exception\");\nB:    catch(ArithmeticException e){}\n     }\n  }<\/pre>\n<p>&nbsp;<\/p>\n<p style=\"padding-left: 30px\">\u00a0 \u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0The above program shows the presence of the following statement<br \/>\n<em> \u201cSystem.out.println(&#8220;Division by zero causes an exception&#8221;);\u201d<br \/>\n<\/em><span style=\"line-height: 1.5em\">\u00a0 \u00a0 \u00a0 \u00a0 placed between point \u2018A\u2019 and point \u2018B\u2019(which is in between the \u2018try-catch\u2019) results in a compiler error.<\/span><\/p>\n<p style=\"padding-left: 30px\">\u00a0 \u00a0 Consider this for a legal version of the \u2018try-catch\u2019 construct:<\/p>\n<pre style=\"padding-left: 30px\">package whizlabs;\n public class except_2 {\n   public static void main(String[] args) {\n     int i=10,j; \/\/ TODO Auto-generated method stub\n     try{\n       j = i\/0;\n     }\n      catch(ArithmeticException e){\n        System.out.println(\"Division by zero causes an exception\");\n     }\n   }\n }<\/pre>\n<p style=\"padding-left: 30px\"><span style=\"line-height: 1.5em\">This will result in the following output:<br \/>\n<\/span><em>\u201cDivision by zero causes an exception\u201d<\/em><\/p>\n<p>C. Extending on point \u2018a\u2019, <b>the \u2018try\u2019 clause can have either the \u2018catch\u2019 clause or the \u2018finally\u2019 clause or both<\/b>. This is also a legal example in addition to the above example :<\/p>\n<pre style=\"padding-left: 30px\">package whizlabs;\npublic class except_2 {\n  public static void main(String[] args) {\n    int i=10,j; \/\/ TODO Auto-generated method stub\n    try {\n       j = i\/10;\n     }\n      finally{\n         System.out.println(\"We are done\");\n      }\n   }\n}<\/pre>\n<p style=\"padding-left: 30px\">\u00a0In the above example, we see the \u2018try\u2019 clause used along with the \u2018finally\u2019 clause.<br \/>\nThe output of the above program as expected will be as follows:<br \/>\n<em>We are done\u00a0<\/em><\/p>\n<p><b>Declaring exceptions:<br \/>\n<\/b>We saw the legal and illegal forms of \u2018try-catch\u2019 constructs. Next we will see how to declare exceptions. Consider the following example:<\/p>\n<pre style=\"padding-left: 30px\">package whizlabs;\nimport java.io.*;\n public class except_2 {\n   public void except() throws IOException\n   {\n     except_example();\n   }\n   void except_example() throws IOException{\n    }\n }<\/pre>\n<p>The above example shows the way to throw an exception. In order to throw an exception, we use the \u2018throws\u2019 keyword along with the type of exception.<br \/>\nIn the above example, the methods \u2018except()\u2019 and \u2018except_example()\u2019 throw \u2018IOException\u2019. Declaring a method throws an exception, is just an indication that it <i>might <\/i>throw an exception \u2013 it is not a necessity that it must throw the exception.<br \/>\nIn the above example, it can also be noted that both methods are throwing the same exception. Any guesses on why it is so? We will discuss this and other things in yet another blog post.<\/p>\n<h4>Bibliography<br \/>\nSCJP 6. In B. B. Kathy Sierra.<\/h4>\n<h4><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>We will extend our discussion about \u2018Exceptions\u2019 which we had started in an earlier post. These were the key takeaways from our earlier post: Differences between checked and unchecked exceptions Important points regarding \u2018finally\u2019 clause Specific exceptions must be declared before the general exceptions In this post, we will discuss the legal and illegal forms of \u2018try-catch\u2019 constructs and more ways to declare exceptions. A. \u00a0Legal and Illegal forms of \u2018try-catch\u2019 constructs : It is illegal to use \u2018try-catch-finally\u2019 clause without the \u2018catch\u2019 clause or the \u2018finally\u2019 clause. In other words, it is illegal to use the \u2018try\u2019 clause alone. [&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":[983],"class_list":["post-11807","post","type-post","status-publish","format-standard","hentry","category-java","tag-java-exceptions"],"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":0,"uagb_excerpt":"We will extend our discussion about \u2018Exceptions\u2019 which we had started in an earlier post. These were the key takeaways from our earlier post: Differences between checked and unchecked exceptions Important points regarding \u2018finally\u2019 clause Specific exceptions must be declared before the general exceptions In this post, we will discuss the legal and illegal forms&hellip;","_links":{"self":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/11807","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=11807"}],"version-history":[{"count":1,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/11807\/revisions"}],"predecessor-version":[{"id":75995,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/11807\/revisions\/75995"}],"wp:attachment":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/media?parent=11807"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/categories?post=11807"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/tags?post=11807"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}