{"id":171,"date":"2020-11-28T03:42:26","date_gmt":"2020-11-28T03:42:26","guid":{"rendered":"https:\/\/codeflarelimited.com\/blog\/?p=171"},"modified":"2021-05-31T05:38:01","modified_gmt":"2021-05-31T04:38:01","slug":"throw-and-throws-exception-in-java","status":"publish","type":"post","link":"https:\/\/codeflarelimited.com\/blog\/throw-and-throws-exception-in-java\/","title":{"rendered":"Throw And Throws Exception in Java"},"content":{"rendered":"\n<p><strong>Throw<\/strong><\/p>\n\n\n\n<p>Throw is a keyword that is used in <a href=\"https:\/\/java.com\/en\/\" target=\"_blank\" rel=\"noreferrer noopener\">Java<\/a> to declare an exception which is similar to the <a href=\"https:\/\/codeflarelimited.com\/blog\/2020\/11\/13\/exceptions-in-java\/\" target=\"_blank\" rel=\"noreferrer noopener\">try\/catch<\/a> block. It is used to declare an explicit exception inside a block of code or method.<\/p>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">public class AgeCheck {\n void checkAge(int age) {\n  if(age &lt; 18){\n   throw new ArithmeticException(\"Not eligible to vote\");\n  }else {\n   System.out.println(\"Eligible to vote\");\n  }\n }\n\npublic static void main(String args[]){\n\/\/Create an object of the class to access the method\nAgeCheck ageCheck = new AgeCheck();\nageCheck.checkAge(13) \/\/Not eligible to vote\n}\n}<\/code><\/pre>\n\n\n\n<p><strong>Throws<\/strong><\/p>\n\n\n\n<p>Throws is a keyword also as well as a method signature used to declare an exception which might be thrown by the method during the program execution.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">public class DivisionCheck {\n int divide(int a, int b) throws ArithmeticException {\n int result = a\/b;\n return result;\n }\npublic static void main(String args[]){\n\/\/Create an object of the class to access the method\n DivisionCheck divisionCheck = new DivisionCheck();\n try {\n  System.out.println(divisionCheck.divide(15,0));\n }catch(ArithmeticException e){\n  System.out.println(\"You cannot divide by zero);\n }\n}\n}<\/code><\/pre>\n\n\n\n<p>Throws is usually used to check the process of program execution, especially when you are not sure of the input you might get from the user.<\/p>\n\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link\" href=\"https:\/\/codeflarelimited.com\/catalogue\/java-training\" target=\"_blank\" rel=\"noreferrer noopener\">Begin you java programming journey<\/a><\/div>\n<\/div>\n\n\n\n<p><strong>THE END<\/strong>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Throw Throw is a keyword that is used in Java to declare an exception which is similar to<\/p>\n","protected":false},"author":1,"featured_media":172,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[25],"tags":[26,27,43,44],"class_list":["post-171","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-exception","tag-java","tag-throw","tag-throws"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Throw And Throws Exception in Java<\/title>\n<meta name=\"description\" content=\"Throw and throws excepiton in java are keywords that are used to declare an exception which is similar to the try\/catch block.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/codeflarelimited.com\/blog\/throw-and-throws-exception-in-java\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Throw And Throws Exception in Java\" \/>\n<meta property=\"og:description\" content=\"Throw and throws excepiton in java are keywords that are used to declare an exception which is similar to the try\/catch block.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/codeflarelimited.com\/blog\/throw-and-throws-exception-in-java\/\" \/>\n<meta property=\"article:author\" content=\"https:\/\/facebook.com\/codeflretech\" \/>\n<meta property=\"article:published_time\" content=\"2020-11-28T03:42:26+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-05-31T04:38:01+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/codeflarelimited.com\/blog\/wp-content\/uploads\/2020\/11\/anxiety.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t<meta property=\"og:image:height\" content=\"1080\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"codeflare\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@codeflaretech\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"TechArticle\",\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/throw-and-throws-exception-in-java\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/throw-and-throws-exception-in-java\\\/\"},\"author\":{\"name\":\"codeflare\",\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/#\\\/schema\\\/person\\\/7e65653d49add95629f8c1053c5cd76a\"},\"headline\":\"Throw And Throws Exception in Java\",\"datePublished\":\"2020-11-28T03:42:26+00:00\",\"dateModified\":\"2021-05-31T04:38:01+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/throw-and-throws-exception-in-java\\\/\"},\"wordCount\":105,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/throw-and-throws-exception-in-java\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/11\\\/anxiety.jpg\",\"keywords\":[\"exception\",\"java\",\"throw\",\"throws\"],\"articleSection\":[\"java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/throw-and-throws-exception-in-java\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/throw-and-throws-exception-in-java\\\/\",\"url\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/throw-and-throws-exception-in-java\\\/\",\"name\":\"Throw And Throws Exception in Java\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/throw-and-throws-exception-in-java\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/throw-and-throws-exception-in-java\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/11\\\/anxiety.jpg\",\"datePublished\":\"2020-11-28T03:42:26+00:00\",\"dateModified\":\"2021-05-31T04:38:01+00:00\",\"description\":\"Throw and throws excepiton in java are keywords that are used to declare an exception which is similar to the try\\\/catch block.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/throw-and-throws-exception-in-java\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/throw-and-throws-exception-in-java\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/throw-and-throws-exception-in-java\\\/#primaryimage\",\"url\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/11\\\/anxiety.jpg\",\"contentUrl\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/11\\\/anxiety.jpg\",\"width\":1920,\"height\":1080,\"caption\":\"throw and throws exception\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/throw-and-throws-exception-in-java\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"java\",\"item\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/java\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Throw And Throws Exception in Java\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/\",\"name\":\"\",\"description\":\"Sustainable solutions\",\"publisher\":{\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/#organization\",\"name\":\"Codeflare Limited\",\"url\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/11\\\/codeflare.png\",\"contentUrl\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/11\\\/codeflare.png\",\"width\":1040,\"height\":263,\"caption\":\"Codeflare Limited\"},\"image\":{\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/#\\\/schema\\\/person\\\/7e65653d49add95629f8c1053c5cd76a\",\"name\":\"codeflare\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/59cef917c86d965eea581d2747f51bd6382003a68bfce7c8a4dfec98b4cd838d?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/59cef917c86d965eea581d2747f51bd6382003a68bfce7c8a4dfec98b4cd838d?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/59cef917c86d965eea581d2747f51bd6382003a68bfce7c8a4dfec98b4cd838d?s=96&d=mm&r=g\",\"caption\":\"codeflare\"},\"description\":\"Latest tech news and coding tips.\",\"sameAs\":[\"https:\\\/\\\/codeflarelimited.com\\\/blog\",\"https:\\\/\\\/facebook.com\\\/codeflretech\",\"https:\\\/\\\/instagram.com\\\/codeflaretech\",\"https:\\\/\\\/x.com\\\/codeflaretech\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCuBLtiYqsajHdqw0uyt7Ofw?sub_confirmation=1\"],\"url\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/author\\\/watcher\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Throw And Throws Exception in Java","description":"Throw and throws excepiton in java are keywords that are used to declare an exception which is similar to the try\/catch block.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/codeflarelimited.com\/blog\/throw-and-throws-exception-in-java\/","og_locale":"en_US","og_type":"article","og_title":"Throw And Throws Exception in Java","og_description":"Throw and throws excepiton in java are keywords that are used to declare an exception which is similar to the try\/catch block.","og_url":"https:\/\/codeflarelimited.com\/blog\/throw-and-throws-exception-in-java\/","article_author":"https:\/\/facebook.com\/codeflretech","article_published_time":"2020-11-28T03:42:26+00:00","article_modified_time":"2021-05-31T04:38:01+00:00","og_image":[{"width":1920,"height":1080,"url":"https:\/\/codeflarelimited.com\/blog\/wp-content\/uploads\/2020\/11\/anxiety.jpg","type":"image\/jpeg"}],"author":"codeflare","twitter_card":"summary_large_image","twitter_creator":"@codeflaretech","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"TechArticle","@id":"https:\/\/codeflarelimited.com\/blog\/throw-and-throws-exception-in-java\/#article","isPartOf":{"@id":"https:\/\/codeflarelimited.com\/blog\/throw-and-throws-exception-in-java\/"},"author":{"name":"codeflare","@id":"https:\/\/codeflarelimited.com\/blog\/#\/schema\/person\/7e65653d49add95629f8c1053c5cd76a"},"headline":"Throw And Throws Exception in Java","datePublished":"2020-11-28T03:42:26+00:00","dateModified":"2021-05-31T04:38:01+00:00","mainEntityOfPage":{"@id":"https:\/\/codeflarelimited.com\/blog\/throw-and-throws-exception-in-java\/"},"wordCount":105,"commentCount":0,"publisher":{"@id":"https:\/\/codeflarelimited.com\/blog\/#organization"},"image":{"@id":"https:\/\/codeflarelimited.com\/blog\/throw-and-throws-exception-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/codeflarelimited.com\/blog\/wp-content\/uploads\/2020\/11\/anxiety.jpg","keywords":["exception","java","throw","throws"],"articleSection":["java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/codeflarelimited.com\/blog\/throw-and-throws-exception-in-java\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/codeflarelimited.com\/blog\/throw-and-throws-exception-in-java\/","url":"https:\/\/codeflarelimited.com\/blog\/throw-and-throws-exception-in-java\/","name":"Throw And Throws Exception in Java","isPartOf":{"@id":"https:\/\/codeflarelimited.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/codeflarelimited.com\/blog\/throw-and-throws-exception-in-java\/#primaryimage"},"image":{"@id":"https:\/\/codeflarelimited.com\/blog\/throw-and-throws-exception-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/codeflarelimited.com\/blog\/wp-content\/uploads\/2020\/11\/anxiety.jpg","datePublished":"2020-11-28T03:42:26+00:00","dateModified":"2021-05-31T04:38:01+00:00","description":"Throw and throws excepiton in java are keywords that are used to declare an exception which is similar to the try\/catch block.","breadcrumb":{"@id":"https:\/\/codeflarelimited.com\/blog\/throw-and-throws-exception-in-java\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/codeflarelimited.com\/blog\/throw-and-throws-exception-in-java\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codeflarelimited.com\/blog\/throw-and-throws-exception-in-java\/#primaryimage","url":"https:\/\/codeflarelimited.com\/blog\/wp-content\/uploads\/2020\/11\/anxiety.jpg","contentUrl":"https:\/\/codeflarelimited.com\/blog\/wp-content\/uploads\/2020\/11\/anxiety.jpg","width":1920,"height":1080,"caption":"throw and throws exception"},{"@type":"BreadcrumbList","@id":"https:\/\/codeflarelimited.com\/blog\/throw-and-throws-exception-in-java\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/codeflarelimited.com\/blog\/"},{"@type":"ListItem","position":2,"name":"java","item":"https:\/\/codeflarelimited.com\/blog\/java\/"},{"@type":"ListItem","position":3,"name":"Throw And Throws Exception in Java"}]},{"@type":"WebSite","@id":"https:\/\/codeflarelimited.com\/blog\/#website","url":"https:\/\/codeflarelimited.com\/blog\/","name":"","description":"Sustainable solutions","publisher":{"@id":"https:\/\/codeflarelimited.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/codeflarelimited.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/codeflarelimited.com\/blog\/#organization","name":"Codeflare Limited","url":"https:\/\/codeflarelimited.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codeflarelimited.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/codeflarelimited.com\/blog\/wp-content\/uploads\/2020\/11\/codeflare.png","contentUrl":"https:\/\/codeflarelimited.com\/blog\/wp-content\/uploads\/2020\/11\/codeflare.png","width":1040,"height":263,"caption":"Codeflare Limited"},"image":{"@id":"https:\/\/codeflarelimited.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/codeflarelimited.com\/blog\/#\/schema\/person\/7e65653d49add95629f8c1053c5cd76a","name":"codeflare","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/59cef917c86d965eea581d2747f51bd6382003a68bfce7c8a4dfec98b4cd838d?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/59cef917c86d965eea581d2747f51bd6382003a68bfce7c8a4dfec98b4cd838d?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/59cef917c86d965eea581d2747f51bd6382003a68bfce7c8a4dfec98b4cd838d?s=96&d=mm&r=g","caption":"codeflare"},"description":"Latest tech news and coding tips.","sameAs":["https:\/\/codeflarelimited.com\/blog","https:\/\/facebook.com\/codeflretech","https:\/\/instagram.com\/codeflaretech","https:\/\/x.com\/codeflaretech","https:\/\/www.youtube.com\/channel\/UCuBLtiYqsajHdqw0uyt7Ofw?sub_confirmation=1"],"url":"https:\/\/codeflarelimited.com\/blog\/author\/watcher\/"}]}},"jetpack_featured_media_url":"https:\/\/codeflarelimited.com\/blog\/wp-content\/uploads\/2020\/11\/anxiety.jpg","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/posts\/171","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/comments?post=171"}],"version-history":[{"count":2,"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/posts\/171\/revisions"}],"predecessor-version":[{"id":661,"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/posts\/171\/revisions\/661"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/media\/172"}],"wp:attachment":[{"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/media?parent=171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/categories?post=171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/tags?post=171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}