{"id":1119,"date":"2016-03-16T07:21:22","date_gmt":"2016-03-16T07:21:22","guid":{"rendered":"http:\/\/abhiandroid.com\/java\/?p=1119"},"modified":"2016-03-16T07:22:49","modified_gmt":"2016-03-16T07:22:49","slug":"exception-handling-in-method-overriding","status":"publish","type":"post","link":"https:\/\/abhiandroid.com\/java\/exception-handling-in-method-overriding.html","title":{"rendered":"Exception Handling In Method Overriding"},"content":{"rendered":"<p>Exceptions are the customary way in java to a calling method that an abnormal condition has occurred. When a method encounters an abnormal condition that it can&#8217;t handle itself, it may throw an exception. Throwing an exception is like throwing or\u00a0flashing red ball to indicate that there is a problem that can&#8217;t be handled where it occurred. Exceptions are caught by handlers which are positioned with\u00a0the thread&#8217;s method invocation stack. If the calling method isn&#8217;t prepared to catch the exception, it throws the exception on\u00a0its calling method and so on. In this article\u00a0we are going to discuss about <a href=\"\/java\/method-overriding\">method overriding<\/a>\u00a0with exception handling in detail.<\/p>\n<p>There are few points to remember when overriding a method with exception handling.<\/p>\n<ul>\n<li>If base class method throws an exception, then child class overridden method can throw the same,\u00a0child class exception or no exception, but it does not throw parent exception that is thrown by base class method.<\/li>\n<li>If the base class method does not throw an exception, then child class overridden method cannot throw the checked exception but it can throw unchecked exception.<\/li>\n<\/ul>\n<hr \/>\n<h4><strong>Program Examples of \u00a0Exception Handling\u00a0in\u00a0Method Overriding<\/strong><\/h4>\n<p><span style=\"color: #008000;\"><strong>Program 1:<\/strong><\/span> This is a program of subclass overridden method declaring <strong>Checked Exception<\/strong>.<\/p>\n<p><strong>Step 1: <\/strong>First we create a class <strong>Parent<\/strong> in which we declare method display():<\/p>\n<pre>import java.io.*;\r\n\r\nclass Parent\r\n\r\n{\r\n\r\n   void display()\r\n\r\n   {\r\n\r\n     System.out.println(\"parent class called\");\r\n\r\n   }\r\n\r\n}<\/pre>\n<p><strong>Step 2: <\/strong>Second we create a class <strong>Child <\/strong>which extends the class <strong>Parent<\/strong> in which we try to override the superclass method which throws IO Exception:<\/p>\n<pre>class Child extends Parent\r\n\r\n{\r\n\r\n   void display() throws IOException\u00a0\u00a0\u00a0 \/\/shows compile time error\r\n\r\n   {\r\n\r\n     System.out.println(\"child class called\");\r\n\r\n   }\r\n\r\n   public static void main( String[] args )\r\n\r\n   {\r\n\r\n     Parent p=new Child();\r\n\r\n     p.display();\r\n\r\n   }\r\n\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>Error: display() in child cannot override display() in Parent\r\nOverridden method does not throw IOException<\/pre>\n<hr \/>\n<p><span style=\"color: #008000;\"><strong>Program 2:<\/strong><\/span> This is a program of subclass overridden method declaring <strong>Unchecked Exception<\/strong>.<\/p>\n<p><strong>Step 1: <\/strong>First we create a class <strong>Parent<\/strong> in which we declare method display():<\/p>\n<pre>import java.io.*;\r\n\r\nclass Parent\r\n\r\n{\r\n\r\n   void display()\r\n\r\n   {\r\n\r\n     System.out.println(\"parent class called\");\r\n\r\n   }\r\n\r\n}<\/pre>\n<p><strong>Step 2: <\/strong>Second we create a class <strong>Child <\/strong>which extends the class<strong> Parent <\/strong>in which we try to override the superclass method which throws ArrayIndexOutOfBoundsException:<\/p>\n<pre>class Child extends Parent\r\n\r\n{\r\n\r\n   void display() throws ArrayIndexOutOfBoundsException\r\n\r\n  {\r\n\r\n    System.out.println(\"child class called\");\r\n\r\n  }\r\n\r\n  public static void main(String[] args)\r\n\r\n  {\r\n\r\n    Parent p=new Child();\r\n\r\n    p.display();\r\n\r\n  }\r\n\r\n}<\/pre>\n<p><strong>Output :<\/strong><\/p>\n<pre>child class called<\/pre>\n<p><strong>Explanation:<\/strong><\/p>\n<p>In this program, the\u00a0unchecked exception is ArrayIndexOutOfBoundsException. Therefore, overrided\u00a0display()\u00a0method can throw it.<\/p>\n<hr \/>\n<p><span style=\"color: #008000;\"><strong>Program 3:<\/strong><\/span> This is a program of subclass overridden method with <strong>same Exception<\/strong>.<\/p>\n<p><strong>Step 1: <\/strong>First we create a class <strong>Parent<\/strong> in which we declare method display() which throws an exception:<\/p>\n<pre>import java.io.*;\r\n\r\nclass Parent\r\n\r\n{\r\n\r\n   void display() throws Exception\r\n\r\n  {\r\n\r\n    System.out.println(\"parent class called\");\r\n\r\n  }\r\n\r\n}<\/pre>\n<p><strong>Step 2: <\/strong>Second we create a class <strong>Child <\/strong>which extends class <strong>Parent <\/strong>in which we try to override the superclass method which throws Exception:<\/p>\n<pre>class Child extends Parent\r\n\r\n{\r\n\r\n  void display() throws Exception\r\n\r\n  {\r\n\r\n    System.out.println(\"child class called\");\r\n\r\n  }\r\n\r\n  public static void main(String[] args)\r\n\r\n  {\r\n\r\n    try {\r\n\r\n    Parent p=new Child();\r\n\r\n    p.display();\r\n\r\n    }\r\n\r\n    catch(Exception e){}\r\n\r\n  }\r\n\r\n}<\/pre>\n<p><strong>Output: <\/strong><\/p>\n<pre>child class called<\/pre>\n<hr \/>\n<p><span style=\"color: #008000;\"><strong>Program 4:<\/strong><\/span> This is a program of subclass overridden method with <strong>no Exception<\/strong>.<\/p>\n<p><strong>Step 1: <\/strong>First we create a class <strong>Parent<\/strong> in which we declare method display() which throws exception:<\/p>\n<pre>import java.io.*;\r\n\r\nclass Parent\r\n\r\n{\r\n\r\n  void display() throws Exception\r\n\r\n  {\r\n\r\n    System.out.println(\"parent class called\");\r\n\r\n  }\r\n\r\n}\r\n<\/pre>\n<p><strong>Step 2: <\/strong>Second we create a class <strong>Child <\/strong>which extends class <strong>Parent <\/strong>in which we try to override the superclass method:<\/p>\n<pre>class Child extends Parent\r\n\r\n{\r\n\r\n  void display()\r\n\r\n  {\r\n\r\n    System.out.println(\"child class called\");\r\n\r\n  }\r\n\r\n  public static void main(String[] args)\r\n\r\n  {\r\n\r\n    try {\r\n\r\n    Parent p=new Child();\r\n\r\n    p.display();\r\n\r\n    }\r\n\r\n    catch(Exception e){}\r\n\r\n  }\r\n\r\n}<\/pre>\n<p><strong>Output: <\/strong><\/p>\n<pre>child class called<\/pre>\n<hr \/>\n<p><span style=\"color: #008000;\"><strong>Program 5:<\/strong><\/span> This is a program of subclass overridden method with <strong>parent Exception<\/strong>.<\/p>\n<p><strong>Step 1: <\/strong>First we create a class <strong>Parent<\/strong> in which we declare method display() which throws ArithmeticException:<\/p>\n<pre>import java.io.*;\r\n\r\nclass Parent\r\n\r\n{\r\n\r\n  void display() throws ArithmeticException\r\n\r\n  {\r\n\r\n    System.out.println(\"parent class called\");\r\n\r\n  }\r\n\r\n}<\/pre>\n<p><strong>Step 2: <\/strong>Second we create a class <strong>Child <\/strong>which extends class <strong>Parent <\/strong>in which we try to override the superclass method which throws exception:<\/p>\n<pre>class Child extends Parent\r\n\r\n{\r\n\r\n  void display() throws Exception\r\n\r\n  {\r\n\r\n    System.out.println(\"child class called\");\r\n\r\n  }\r\n\r\n  public static void main(String[] args)\r\n\r\n  {\r\n\r\n    try {\r\n\r\n    Parent p=new Child();\r\n\r\n    s.display();\r\n\r\n    }\r\n\r\n    catch(Exception e){}\r\n\r\n  }\r\n\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>Error: display() in child cannot override display() in Parent\r\nOverridden method does not throw Exception<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Exceptions are the customary way in java to a calling method that an abnormal condition has occurred. When a method encounters an abnormal condition that it can&#8217;t handle itself, it may throw an exception. Throwing an exception is like throwing or\u00a0flashing red ball to indicate that there is a problem that can&#8217;t be handled where &hellip; <a href=\"https:\/\/abhiandroid.com\/java\/exception-handling-in-method-overriding.html\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Exception Handling In Method Overriding<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,79],"tags":[],"class_list":["post-1119","post","type-post","status-publish","format-standard","hentry","category-archieve","category-java-basics"],"psp_head":"<title>Exception Handling In Method Overriding \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Exceptions are the customary way in java to a calling method that an abnormal condition has occurred. When a method encounters an abnormal condition that it can&#039;t handle itself, it may throw an exception.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/java\/exception-handling-in-method-overriding.html\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts\/1119","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/comments?post=1119"}],"version-history":[{"count":0,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts\/1119\/revisions"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/media?parent=1119"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/categories?post=1119"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/tags?post=1119"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}