{"id":4345,"date":"2014-03-30T09:57:06","date_gmt":"2014-03-30T09:57:06","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/declare-and-throw-vs-throw-without-being-declared-exceptions-collection-of-common-programming-errors\/"},"modified":"2014-03-30T09:57:06","modified_gmt":"2014-03-30T09:57:06","slug":"declare-and-throw-vs-throw-without-being-declared-exceptions-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/declare-and-throw-vs-throw-without-being-declared-exceptions-collection-of-common-programming-errors\/","title":{"rendered":"Declare-and-throw vs. throw-without-being-declared exceptions-Collection of common programming errors"},"content":{"rendered":"<p>Aside from the good answers given by others about the compilability of your methods by themselves, there is the matter of implementing interfaces and overriding methods in super-classes.<\/p>\n<p>The rule says that you can override\/implement a method, but <strong>you cannot declare additional exception<\/strong> to those declared by the original method signature. To understand this, think of this example.<\/p>\n<p>Let&#8217;s say you&#8217;re using some sort of data structure:<\/p>\n<pre><code>public abstract class AbstractBox {\n  public abstract void addItem(Item newItem);\n  public abstract void removeItem(Item oldItem);\n}\n<\/code><\/pre>\n<p>You have your own implementation, but you decide to declare exceptions that do not appear in the original signature:<\/p>\n<pre><code>public class MyBox extends AbstractBox {\n  public void addItem(Item newItem) throws ItemAlreadyPresentException {...}\n  public void removeItem(Item oldItem) throws NoSuchItemException {...}\n}\n<\/code><\/pre>\n<p>Now let&#8217;s consider this generic code that handles Box objects, and receives an instance of MyBox:<\/p>\n<pre><code>public void boxHandler(AbstractBox box) {\n  Item item = new Item();\n  box.removeItem(item);\n}\n<\/code><\/pre>\n<p>Whoever wrote this code wasn&#8217;t expecting any exceptions, nor did she intend to handle implementor exceptions. To prevent this, the compiler will not allow you to declare additional exceptions to those in the original signature.<\/p>\n<p>Of course, if you handle exceptions internally&#8230; well, the compiler will be more than happy to allow you to drop declared exceptions from your signature \ud83d\ude09<\/p>\n<p>Hope this helps&#8230;<\/p>\n<p>Yuval =8-)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Aside from the good answers given by others about the compilability of your methods by themselves, there is the matter of implementing interfaces and overriding methods in super-classes. The rule says that you can override\/implement a method, but you cannot declare additional exception to those declared by the original method signature. To understand this, think [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4345","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4345","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4345"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4345\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4345"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4345"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}