Table of Contents
Talking about throw and throws, both are the concepts of exception handling. Throw is used to explicitly throw an exception from a method or any block of code. On the other hand, throws are used in the signature of the method to indicate that this method might throw one of the listed type exceptions.
What is Throw in Java?
Throw keyword in java is used to throw an exception explicitly and logically defined by the programmer during the program control moves from one block to another assuming the errors exception are defined and handled accordingly.
What is Throws in Java?
1: What is the default value of a boolean in Java?
Throws is used to declare and call an exception block, which means its working is similar to the try-catch block.
Learn Coding in your Language! Enroll Here!
Differences between throw and throws
Let us look at table given below to have a clear look at what are the differences between throw and throws in Java:
Sr. no. | Basis of Differences | throw | throws |
1 | Definition | Java throw keyword is used throw an exception explicitly in the code, inside the function or the block of code. |
Java throws keyword is used in the method signature to declare an exception which might be thrown by the function while the execution of the code.
|
2 | Type of exception Using throw keyword, we can only propagate unchecked exception i.e., the checked exception cannot be propagated using throw only. |
Using throws keyword, we can declare both checked and unchecked exceptions. However, the throws keyword can be used to propagate checked exceptions only.
|
|
3 | Syntax | The throw keyword is followed by an instance of Exception to be thrown. |
The throws keyword is followed by class names of Exceptions to be thrown.
|
4 | Declaration | throw is used within the method. |
throws is used with the method signature.
|
5 | Internal implementation | We are allowed to throw only one exception at a time i.e. we cannot throw multiple exceptions. |
We can declare multiple exceptions using throws keyword that can be thrown by the method. For example, main() throws IOException, SQLException.
|
Examples for throw and throws
Check below for examples on throw and throws –
- throw exception in 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)
Learn to code from industry experts! Enroll here
- throws keyword in 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)
You will be provided with an online platform to prepare for the exam. Entri provides video classes as well on various important topics by the excellent faculties. You can download the app free of cost and join the class. One will get revision modules, monthly tests based on the classes.