{"id":140,"date":"2020-03-10T06:44:00","date_gmt":"2020-03-10T06:44:00","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=140"},"modified":"2024-02-02T17:23:51","modified_gmt":"2024-02-02T22:23:51","slug":"creating-custom-exceptions-in-java","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/creating-custom-exceptions-in-java\/","title":{"rendered":"Creating custom exceptions in Java"},"content":{"rendered":"\n<p>Java allows programmers to create their own Exceptions. To create and exception, you should inherit from the exception closest to what you wish to create. Following is a generic example to show how to create and use a custom exception.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class CustomException extends Exception\n{\n   String ex;\n   public CustomException() {\n       super();\n       ex = \"Something is empty\";\n   }\n\n   public CustomException(String msg) {\n       super(msg);\n       ex = msg;\n   }\n\n   public String getError() {\n       return ex;\n   }\n}<\/code><\/pre>\n\n\n\n<p>This exception class provides a no argument constructor which prints a default message, &#8220;Something is empty&#8221;. The second constructor returns the message in the throw statement of the calling method. getError() is a callable function that returns the Exception message.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class TestCustomException\n{\n   public static void main(String&#91;] args) {\n       try {\n           f();\n       } catch (CustomException e) {\n           System.out.println(e.getError());\n       }\n\n       try {\n           g();\n       } catch (CustomException e) {\n           System.out.println(e.getError());\n       }\n   }\n\n   public static void f() throws CustomException { \n       throw new CustomException();\n   }\n\n   public static void g() throws CustomException { \n       throw new CustomException(\"empty\");\n   }\n}<\/code><\/pre>\n\n\n\n<p>output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Something is empty\nempty<\/code><\/pre>\n\n\n\n<p>Method f() throws a CustomException without any argument, so the default value message &#8220;Something is empty&#8221; is returned. Method g() throws a CustomException with a String argument.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java allows programmers to create their own Exceptions. To create and exception, you should inherit from the exception closest to what you wish to create. Following is a generic example to show how to create and use a custom exception. This exception class provides a no argument constructor which prints a default message, &#8220;Something is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37],"tags":[38],"class_list":["post-140","post","type-post","status-publish","format-standard","hentry","category-java","tag-java"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/140","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/comments?post=140"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/140\/revisions"}],"predecessor-version":[{"id":141,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/140\/revisions\/141"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=140"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}