{"id":4810,"date":"2023-12-01T13:36:39","date_gmt":"2023-12-01T13:36:39","guid":{"rendered":"https:\/\/codethataint.com\/blog\/?p=4810"},"modified":"2023-12-01T13:51:16","modified_gmt":"2023-12-01T13:51:16","slug":"writing-custom-exception-in-java","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/writing-custom-exception-in-java\/","title":{"rendered":"Writing Custom Exception in Java"},"content":{"rendered":"<p><strong>Why we need Custom Exception<\/strong><br \/>\nExceptions are technical causes which should be wrapped in a presentable way with a specific business logic and workflow. adding a higher abstraction layer for the exception handling, which results in more meaningful and readable API<\/p>\n<p><strong>How to write Custom Exception<\/strong><br \/>\nAll you need to do is create a new class and have it extend Exception. If you want an Exception that is unchecked, you need to extend RuntimeException.<br \/>\nNote: A checked Exception is one that requires you to either surround the Exception in a try\/catch block or have a &#8216;throws&#8217; clause on the method declaration. (like IOException) Unchecked Exceptions may be thrown just like checked Exceptions, but you aren&#8217;t required to explicitly handle them in any way (IndexOutOfBoundsException).<\/p>\n<p><em>Exception, if you want your exception to be checked (i.e: required in a throws clause).RuntimeException, if you want your exception to be unchecked.<\/em><\/p>\n<p><strong>Creating Custom Checked Exception<\/strong><br \/>\n<em>Note:<\/em>constructor takes a Throwable\u2019s subclass which is the origin (cause) of the current exception<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class StudentStoreException extends Exception {\r\n     public StudentStoreException(String message, Throwable cause) {\r\n        super(message, cause);\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Creating Custom Unchecked Exception<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class IncorrectFileExtensionException \r\n  extends RuntimeException {\r\n    public IncorrectFileExtensionException(String errorMessage, Throwable err) {\r\n        super(errorMessage, err);\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Methods in Custom Exception<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class MyOwnException extends Exception {\r\n    public MyOwnException() {\r\n\r\n    }\r\n\r\n    public MyOwnException(String message) {\r\n        super(message);\r\n    }\r\n\r\n    public MyOwnException(Throwable cause) {\r\n        super(cause);\r\n    }\r\n\r\n    public MyOwnException(String message, Throwable cause) {\r\n        super(message, cause);\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Why we need Custom Exception Exceptions are technical causes which should be wrapped in a presentable way with a specific business logic and workflow. adding a higher abstraction layer for the exception handling, which results in more meaningful and readable API How to write Custom Exception All you need to do is create a new&hellip; <a href=\"https:\/\/codethataint.com\/blog\/writing-custom-exception-in-java\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[267,28],"tags":[],"class_list":["post-4810","post","type-post","status-publish","format-standard","hentry","category-interview-questions-exceptions","category-exceptions"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4810","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=4810"}],"version-history":[{"count":1,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4810\/revisions"}],"predecessor-version":[{"id":4811,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4810\/revisions\/4811"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=4810"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=4810"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=4810"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}