Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Difference Between throw and throws in Java

  • Difficulty Level : Basic
  • Last Updated : 07 Dec, 2021

Prerequisite: Throw and Throws in Java

The throw and throws are the concepts of exception handling in Java where the throw keyword throws the exception explicitly from a method or a block of code, whereas the throws keyword is used in the signature of the method.

The differences between throw and throws in Java are:

S. No.

Key Difference



throw

throws

1.Point of UsageThe throw keyword is used inside a function. It is used when it is required to throw an Exception logically.The throws keyword is used in the function signature. It is used when the function has some statements that can lead to exceptions.
2.Exceptions ThrownThe throw keyword is used to throw an exception explicitly. It can throw only one exception at a time.The throws keyword can be used to declare multiple exceptions, separated by a comma. Whichever exception occurs, if matched with the declared ones, is thrown automatically then.
3.SyntaxSyntax of throw keyword includes the instance of the Exception to be thrown. Syntax wise throw keyword is followed by the instance variable.Syntax of throws keyword includes the class names of the Exceptions to be thrown. Syntax wise throws keyword is followed by exception class names.
4.Propagation of Exceptionsthrow keyword cannot propagate checked exceptions. It is only used to propagate the unchecked Exceptions that are not checked using the throws keyword. throws keyword is used to propagate the checked Exceptions only. 

Examples

1. Java throw

Java




// Java program to demonstrate the working 
// of throw keyword in exception handling
  
public class GFG {
    public static void main(String[] args)
    {
        // Use of unchecked Exception
        try {
            // double x=3/0;
            throw new ArithmeticException();
        }
        catch (ArithmeticException e) {
            e.printStackTrace();
        }
    }
}

Output: 

java.lang.ArithmeticException
    at GFG.main(GFG.java:10)

2. Java throws

Java




// Java program to demonstrate the working
// of throws keyword in exception handling
import java.io.*;
import java.util.*;
  
public class GFG {
  
    public static void writeToFile() throws Exception
    {
        BufferedWriter bw = new BufferedWriter(
            new FileWriter("myFile.txt"));
        bw.write("Test");
        bw.close();
    }
  
    public static void main(String[] args) throws Exception
    {
        try {
            writeToFile();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Output:

java.security.AccessControlException: access denied ("java.io.FilePermission" "myFile.txt" "write")
  at GFG.writeToFile(GFG.java:10)

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!