{"id":4066,"date":"2026-11-21T11:54:03","date_gmt":"2026-11-21T11:54:03","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=4066"},"modified":"2026-11-21T11:54:07","modified_gmt":"2026-11-21T11:54:07","slug":"difference-between-throw-and-throws-in-java","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/difference-between-throw-and-throws-in-java\/","title":{"rendered":"Difference Between throw and throws in Java (with Examples)"},"content":{"rendered":"<p><code>throw<\/code> and <code>throws<\/code> look almost the same, and both belong to exception handling in Java, but they do different jobs. <strong><code>throw<\/code> actually raises an exception<\/strong> at a point inside your code. <strong><code>throws<\/code> only declares<\/strong>, in a method&#8217;s signature, that the method might pass certain exceptions to whoever calls it.<\/p>\n<h2>throw vs throws at a Glance<\/h2>\n<table>\n<thead>\n<tr>\n<th><\/th>\n<th><code>throw<\/code><\/th>\n<th><code>throws<\/code><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>What it does<\/td>\n<td>Raises an exception right now<\/td>\n<td>Warns callers that the method may pass on an exception<\/td>\n<\/tr>\n<tr>\n<td>Where it is written<\/td>\n<td>Inside a method or block<\/td>\n<td>In the method declaration, after the parameter list<\/td>\n<\/tr>\n<tr>\n<td>Followed by<\/td>\n<td>An exception <strong>object<\/strong>: <code>throw new IllegalArgumentException(\"...\")<\/code><\/td>\n<td>Exception <strong>class names<\/strong>: <code>throws IOException<\/code><\/td>\n<\/tr>\n<tr>\n<td>How many<\/td>\n<td>One exception object at a time<\/td>\n<td>Several, separated by commas<\/td>\n<\/tr>\n<tr>\n<td>Effect on the program<\/td>\n<td>Normal flow stops immediately<\/td>\n<td>None by itself; it is information for the compiler and the caller<\/td>\n<\/tr>\n<tr>\n<td>Typical use<\/td>\n<td>Rejecting invalid input, signalling a business rule failure, re-throwing<\/td>\n<td>Declaring checked exceptions you do not want to handle inside the method<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>The throw Keyword<\/h2>\n<p>Use <code>throw<\/code> when your code detects a problem and wants to stop and report it. The syntax is:<\/p>\n<pre class=\"wp-block-code\"><code>throw new ExceptionType(\"message\");<\/code><\/pre>\n<pre class=\"wp-block-code\"><code>public class ThrowDemo {\n    static void setAge(int age) {\n        if (age &lt; 0) {\n            throw new IllegalArgumentException(\"Age cannot be negative: \" + age);\n        }\n        System.out.println(\"Age set to \" + age);\n    }\n\n    public static void main(String[] args) {\n        setAge(21);\n        try {\n            setAge(-5);\n        } catch (IllegalArgumentException e) {\n            System.out.println(\"Caught: \" + e.getMessage());\n        }\n    }\n}<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>Age set to 21\nCaught: Age cannot be negative: -5<\/code><\/pre>\n<p>If you remove the <code>try-catch<\/code>, nothing catches the exception and the program ends with an error that begins like this, followed by the stack trace:<\/p>\n<pre class=\"wp-block-code\"><code>Exception in thread \"main\" java.lang.IllegalArgumentException: Age cannot be negative: -5<\/code><\/pre>\n<p><code>IllegalArgumentException<\/code> is an <em>unchecked<\/em> exception, so <code>setAge()<\/code> did not need a <code>throws<\/code> clause.<\/p>\n<h2>The throws Keyword<\/h2>\n<p>Java forces you to deal with <em>checked<\/em> exceptions, such as <code>IOException<\/code>. You have two choices: catch the exception inside the method, or declare it with <code>throws<\/code> and let the caller handle it.<\/p>\n<pre class=\"wp-block-code\"><code>import java.io.FileReader;\nimport java.io.IOException;\n\npublic class ThrowsDemo {\n    static void openFile(String name) throws IOException {\n        try (FileReader reader = new FileReader(name)) {\n            System.out.println(\"File opened: \" + name);\n        }\n    }\n\n    public static void main(String[] args) {\n        try {\n            openFile(\"notes.txt\");\n        } catch (IOException e) {\n            System.out.println(\"Could not open file: \" + e.getMessage());\n        }\n    }\n}<\/code><\/pre>\n<p><strong>Output<\/strong> (when <code>notes.txt<\/code> does not exist, on Linux or macOS):<\/p>\n<pre class=\"wp-block-code\"><code>Could not open file: notes.txt (No such file or directory)<\/code><\/pre>\n<p>On Windows the message ends with &#8220;(The system cannot find the file specified)&#8221; instead.<\/p>\n<p>If you delete <code>throws IOException<\/code> from <code>openFile()<\/code> and do not catch the exception there either, the code will not compile. The compiler reports an error like:<\/p>\n<pre class=\"wp-block-code\"><code>error: unreported exception FileNotFoundException; must be caught or declared to be thrown<\/code><\/pre>\n<h2>Using throw and throws Together: A Custom Exception<\/h2>\n<p>The two keywords often appear together. Here a bank-style method <code>throw<\/code>s a custom checked exception and declares it with <code>throws<\/code>:<\/p>\n<pre class=\"wp-block-code\"><code>class InsufficientBalanceException extends Exception {\n    public InsufficientBalanceException(String message) {\n        super(message);\n    }\n}\n\npublic class BankDemo {\n    static int balance = 500;\n\n    static void withdraw(int amount) throws InsufficientBalanceException {\n        if (amount &gt; balance) {\n            throw new InsufficientBalanceException(\n                \"Insufficient balance. Available: \" + balance + \", requested: \" + amount);\n        }\n        balance -= amount;\n        System.out.println(\"Withdrawn: \" + amount + \", balance left: \" + balance);\n    }\n\n    public static void main(String[] args) {\n        try {\n            withdraw(200);\n            withdraw(800);\n        } catch (InsufficientBalanceException e) {\n            System.out.println(\"Error: \" + e.getMessage());\n        }\n    }\n}<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>Withdrawn: 200, balance left: 300\nError: Insufficient balance. Available: 300, requested: 800<\/code><\/pre>\n<p>Because <code>InsufficientBalanceException<\/code> extends <code>Exception<\/code> (not <code>RuntimeException<\/code>), it is checked, so <code>withdraw()<\/code> must declare it with <code>throws<\/code>.<\/p>\n<h2>Declaring More Than One Exception<\/h2>\n<pre class=\"wp-block-code\"><code>void loadData(String path) throws IOException, InterruptedException {\n    \/\/ reading a file and waiting for another thread\n}<\/code><\/pre>\n<h2>Re-throwing an Exception<\/h2>\n<p><code>throw<\/code> can also be used inside a <code>catch<\/code> block, for example to wrap a low-level exception in a clearer one while keeping the original as the cause:<\/p>\n<pre class=\"wp-block-code\"><code>try {\n    openFile(\"settings.txt\");\n} catch (IOException e) {\n    throw new RuntimeException(\"Could not load settings\", e);\n}<\/code><\/pre>\n<h2>Rules to Remember<\/h2>\n<ul>\n<li>You can only <code>throw<\/code> objects of <code>Throwable<\/code> or its subclasses.<\/li>\n<li>Any statement written directly after <code>throw<\/code> in the same block is unreachable, and the compiler rejects it.<\/li>\n<li>Checked exceptions must be either caught or declared with <code>throws<\/code>. Unchecked exceptions (subclasses of <code>RuntimeException<\/code> and <code>Error<\/code>) do not need to be declared.<\/li>\n<li>An overriding method cannot declare new or broader checked exceptions than the method it overrides.<\/li>\n<li><code>throws<\/code> does not handle anything. Somewhere up the call chain, the exception still has to be caught, or the program stops.<\/li>\n<\/ul>\n<h2>Frequently Asked Questions<\/h2>\n<h3>What is the main difference between throw and throws in Java?<\/h3>\n<p><code>throw<\/code> creates and raises an exception inside a method. <code>throws<\/code> appears in the method signature and declares which exceptions the method may pass to its caller.<\/p>\n<h3>Can we throw multiple exceptions with throw?<\/h3>\n<p>No, one <code>throw<\/code> statement throws one exception object. You can, however, list several exception classes after <code>throws<\/code>.<\/p>\n<h3>Is throws necessary for unchecked exceptions?<\/h3>\n<p>No. You may write it for documentation, but the compiler only requires it for checked exceptions.<\/p>\n<h3>Can main() use throws?<\/h3>\n<p>Yes, for example <code>public static void main(String[] args) throws IOException<\/code>. If the exception reaches <code>main<\/code> and is not caught, the program ends and prints the stack trace.<\/p>\n<p>Related: <a href=\"https:\/\/gyanipandit.com\/programming\/java-methods\/\">Java methods<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>throw and throws look almost the same, and both belong to exception handling in Java, but they do different jobs. throw actually raises an exception at a point inside your code. throws only declares, in a method&#8217;s signature, that the method might pass certain exceptions to whoever calls it. throw vs throws at a Glance &#8230; <a title=\"Difference Between throw and throws in Java (with Examples)\" class=\"read-more\" href=\"https:\/\/gyanipandit.com\/programming\/difference-between-throw-and-throws-in-java\/\" aria-label=\"Read more about Difference Between throw and throws in Java (with Examples)\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[12],"class_list":["post-4066","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-java"],"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/4066","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=4066"}],"version-history":[{"count":3,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/4066\/revisions"}],"predecessor-version":[{"id":6151,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/4066\/revisions\/6151"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=4066"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=4066"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=4066"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}