{"id":359308,"date":"2024-05-21T00:29:52","date_gmt":"2024-05-21T00:29:52","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=359308"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=359308","title":{"rendered":"<span>How to Use Throw and Throws in Java<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>Exception handling in Java is the most effective way to handle runtime errors occurring in the application. This is used to protect the abnormal flow of the execution of the application and continue the application in normal flow. This is the process of handling runtime errors such as ClassNotFoundException, IOException, etc. The throw and throws keywords are used to handle exceptions in Java.<br \/>In this topic, we will learn how to use\u00a0<a href=\"https:\/\/springjava.com\/core-java\/how-to-use-throw-and-throws-in-java\" rel=\"noopener noreferrer nofollow\"><em>throw and throws keywords in Java<\/em><\/a>\u00a0with examples.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/dce\/63a\/b51\/dce63ab513df3671e6c993731cdfbf46.png\" width=\"553\" height=\"466\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/dce\/63a\/b51\/dce63ab513df3671e6c993731cdfbf46.png\"\/><\/figure>\n<h3>Java throw\u00a0<\/h3>\n<p>It is a keyword which is used to explicitly throw exceptions and customize the exception as per the need in Java.<\/p>\n<h4>Syntax of Java throw<\/h4>\n<pre><code class=\"java\">throw new exception_class(?message?);<\/code><\/pre>\n<p>\u2192 The \u201cexception_class\u201d \u00a0must be Throwable or a child class of the Throwable.<\/p>\n<h4>Example<\/h4>\n<pre><code class=\"java\">throw new ArithmeticException(\"\/ by zero\");<\/code><\/pre>\n<p>Example of throw keyword in Java<\/p>\n<p>In this example, we will create a custom exception in Java.<\/p>\n<pre><code class=\"java\">class MyException extends Exception { public MyException(String s){  super(s);     } } public class Main { public static void main(String args[]){ try { \/\/throw an object of user-defined exception throw new MyException(\"SpringJava\"); }catch (MyException ex) { System.out.println(\"Caught\"); System.out.println(ex.getMessage());         }     } }<\/code><\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre><code class=\"bash\">Caught SpringJava<\/code><\/pre>\n<p>Java throws<\/p>\n<p>It is a keyword used to delegate the responsibility of exception handling to the caller method then the caller method is responsible for handling that occurred exception.<\/p>\n<h4>Syntax of Java throws<\/h4>\n<pre><code class=\"java\">type method_name(parameters) throws exception_list<\/code><\/pre>\n<p>Examples of Java throws in exception handling\u00a0<\/p>\n<p><strong>Example 1<\/strong><br \/>In this example exception is unhandled.<\/p>\n<pre><code class=\"java\">public class SpringJava{ public static void main(String[] args){ Thread.sleep(10000); System.out.println(\"Hello SpringJava\");     } }<\/code><\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre><code class=\"bash\">\/SpringJava.java:3: error: unreported exception InterruptedException; must be caught or declared to be thrown Thread.sleep(10000);             ^ 1 error<\/code><\/pre>\n<p><strong>Example 2<\/strong><br \/>In this example, an exception is handled with throws.<\/p>\n<pre><code class=\"java\">public class SpringJava{ public static void main(String[] args)throws InterruptedException{ Thread.sleep(10000); System.out.println(\"Hello SpringJava\");     } }<\/code><\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre><code class=\"bash\">Hello SpringJava<\/code><\/pre>\n<p>Example 3<br \/>In this example, an exception is handled with the use of throw, throws and try-catch block.<\/p>\n<pre><code class=\"java\">public class SpringJava{ static void demo() throws IllegalAccessException{ System.out.println(\"inside the demo() method\"); throw new IllegalAccessException(\"Test\"); } public static void main(String args[]){ try { demo(); }catch (IllegalAccessException e) { System.out.println(\"caught in main.\");         }     } }<\/code><\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre><code class=\"bash\">inside the demo() method caught in the main<\/code><\/pre>\n<p>FAQ<\/p>\n<p>Q1: What is the purpose of throw and throws in Java?<br \/>Ans. A throw keyword is used to throw an exception explicitly. This keyword can throw only one exception at a time. A throws keyword can be used to declare multiple exceptions separated by a comma(,).\u00a0<br \/>Q2: Can we use throw without try?<br \/>Ans. We can use the throws keyword for exception handling without using a try-catch block.<br \/>Q3: Why we use throws instead of try catch?<br \/>Ans. We can use the throws keyword to declare the exception with the method declaration. It is used to forcibly throw the exception, while the try-catch block handles the exceptions thrown by the code in Java.<\/p>\n<h4>Conclusion<\/h4>\n<p>In this topic, we learnt\u00a0<strong>how to use throws and throws keywords for exception handling in Java with examples<\/strong>.\u00a0<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/771890\/\"> https:\/\/habr.com\/ru\/articles\/771890\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>Exception handling in Java is the most effective way to handle runtime errors occurring in the application. This is used to protect the abnormal flow of the execution of the application and continue the application in normal flow. This is the process of handling runtime errors such as ClassNotFoundException, IOException, etc. The throw and throws keywords are used to handle exceptions in Java.<br \/>In this topic, we will learn how to use\u00a0<a href=\"https:\/\/springjava.com\/core-java\/how-to-use-throw-and-throws-in-java\" rel=\"noopener noreferrer nofollow\"><em>throw and throws keywords in Java<\/em><\/a>\u00a0with examples.<\/p>\n<figure class=\"full-width\"><\/figure>\n<h3>Java throw\u00a0<\/h3>\n<p>It is a keyword which is used to explicitly throw exceptions and customize the exception as per the need in Java.<\/p>\n<h4>Syntax of Java throw<\/h4>\n<pre><code class=\"java\">throw new exception_class(?message?);<\/code><\/pre>\n<p>\u2192 The \u201cexception_class\u201d \u00a0must be Throwable or a child class of the Throwable.<\/p>\n<h4>Example<\/h4>\n<pre><code class=\"java\">throw new ArithmeticException(\"\/ by zero\");<\/code><\/pre>\n<p>Example of throw keyword in Java<\/p>\n<p>In this example, we will create a custom exception in Java.<\/p>\n<pre><code class=\"java\">class MyException extends Exception { public MyException(String s){  super(s);     } } public class Main { public static void main(String args[]){ try { \/\/throw an object of user-defined exception throw new MyException(\"SpringJava\"); }catch (MyException ex) { System.out.println(\"Caught\"); System.out.println(ex.getMessage());         }     } }<\/code><\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre><code class=\"bash\">Caught SpringJava<\/code><\/pre>\n<p>Java throws<\/p>\n<p>It is a keyword used to delegate the responsibility of exception handling to the caller method then the caller method is responsible for handling that occurred exception.<\/p>\n<h4>Syntax of Java throws<\/h4>\n<pre><code class=\"java\">type method_name(parameters) throws exception_list<\/code><\/pre>\n<p>Examples of Java throws in exception handling\u00a0<\/p>\n<p><strong>Example 1<\/strong><br \/>In this example exception is unhandled.<\/p>\n<pre><code class=\"java\">public class SpringJava{ public static void main(String[] args){ Thread.sleep(10000); System.out.println(\"Hello SpringJava\");     } }<\/code><\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre><code class=\"bash\">\/SpringJava.java:3: error: unreported exception InterruptedException; must be caught or declared to be thrown Thread.sleep(10000);             ^ 1 error<\/code><\/pre>\n<p><strong>Example 2<\/strong><br \/>In this example, an exception is handled with throws.<\/p>\n<pre><code class=\"java\">public class SpringJava{ public static void main(String[] args)throws InterruptedException{ Thread.sleep(10000); System.out.println(\"Hello SpringJava\");     } }<\/code><\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre><code class=\"bash\">Hello SpringJava<\/code><\/pre>\n<p>Example 3<br \/>In this example, an exception is handled with the use of throw, throws and try-catch block.<\/p>\n<pre><code class=\"java\">public class SpringJava{ static void demo() throws IllegalAccessException{ System.out.println(\"inside the demo() method\"); throw new IllegalAccessException(\"Test\"); } public static void main(String args[]){ try { demo(); }catch (IllegalAccessException e) { System.out.println(\"caught in main.\");         }     } }<\/code><\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre><code class=\"bash\">inside the demo() method caught in the main<\/code><\/pre>\n<p>FAQ<\/p>\n<p>Q1: What is the purpose of throw and throws in Java?<br \/>Ans. A throw keyword is used to throw an exception explicitly. This keyword can throw only one exception at a time. A throws keyword can be used to declare multiple exceptions separated by a comma(,).\u00a0<br \/>Q2: Can we use throw without try?<br \/>Ans. We can use the throws keyword for exception handling without using a try-catch block.<br \/>Q3: Why we use throws instead of try catch?<br \/>Ans. We can use the throws keyword to declare the exception with the method declaration. It is used to forcibly throw the exception, while the try-catch block handles the exceptions thrown by the code in Java.<\/p>\n<h4>Conclusion<\/h4>\n<p>In this topic, we learnt\u00a0<strong>how to use throws and throws keywords for exception handling in Java with examples<\/strong>.\u00a0<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/771890\/\"> https:\/\/habr.com\/ru\/articles\/771890\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-359308","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/359308","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=359308"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/359308\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=359308"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=359308"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=359308"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}