Difference between throw and throws in java

Java Throw vs Throws

In this article, we are going to understand and explore about two interesting keywords in Java, which are throw and throws. We would see what are the points of differences between the keywords throw and throws, and we would see some throw and throws in java examples, through which, we can understand the concept better. The thing is that both the keywords – throw and throws, are used in exception handling.

Difference between Throw and Throws in Java

Java Throw Keyword

At times, when we are required to throw exceptions in java explicitly in our program, we can simply make use of the throw keyword in our Java programs. We just need to use the throw keyword, and then we specify what exception we want to throw.

We can throw checked or unchecked exceptions in Java using the throw keyword, but it is mainly used to throw some custom exceptions.

Let’s have a look at a simple example, through which, we can understand the throw keyword.

In the below example, let’s take a number from the user as input, and if the number is less than zero, we would throw an ArithmeticException(just for demonstration), with a message that numbers less than zero are not allowed. Have a look at the below program.

import java.util.Scanner;

public class Example {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println(“Please enter a positive number:”);
int number = scanner.nextInt();
if (number<0)
throw new ArithmeticException(“Numbers less than zero are not allowed”);
else
System.out.println(“Everything is fine!”);
}
}

As you can see in the above program, we are just taking a number as user input, and then we are checking if the number is less than zero, we are throwing an ArithmeticException, with the message, that numbers less than zero are not allowed.

You can try to execute the above program for different inputs, but here, let’s have a look at the sample input outputs of the above program –

Please enter a positive number:
4
Everything is fine!

As you can see in the above program, we have input a positive number, so here we have no problem. But if we try to input some negative number, the ArithmeticException is raised. Have a look at this condition –

Please enter a positive number:
-4
Exception in thread “main” java.lang.ArithmeticException: Numbers less than zero are not allowed
at com.gyanipandit.Example.main(Example.java:11)

As you can see from the output, when we are giving a negative number as input, we are getting into an error. You can also try for other inputs.

Java Throws Keyword

As stated earlier, the throws keyword is used in exception handling, the throws keyword is used in the method declaration, to declare what type of java throws exception might be thrown from this method. This is the kind of information, that an exception might occur here.

We would see an example, through which, we can better understand the situations when we would use the throws keyword.

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;

public class Example {
public static void fileOperation() throws FileNotFoundException {
File file = new File(“gyanipandit.txt”);
// this code may throw FileNotFoundException
FileReader fileReader = new FileReader(file);
}

public static void main(String[] args) {
try{
fileOperation();
}
catch(FileNotFoundException f){
System.out.println(f);
}
}
}

As you can see in the above program, we have created a method with the name fileOperation, and within this method, we have an instruction, which may throw FileNotFoundException, which is a checked exception. Since it is a checked exception, we just need to handle it, so we can either handle it ourselves, or we can also declare it to be thrown(as can be seen in the above program).

We can also declare multiple exceptions to be thrown from the method declaration, writing the other exceptions separated by commas.

So, from the above throw and throws in Java example, we can understand that both the keywords – throw and throws, are used in exception handling. The throw keyword is used in situations when we are required to throw some exception explicitly. Let’s have a look at the table below, which states some other points of difference between the throw and throws keywords.

Difference between throw and throws in Java

throw keywordthrows keyword
The throw keyword is used to throw exceptions explicitlyThe throws keyword is used with the method signature, to declare that there might be some exception.
The throw keyword is followed by an instance, like throw new ArithmeticException(“Exception”)The throws keyword is followed by the exception class name. For example, throws FileNotFoundException.
We can only throw one exception at a time, with the throw keyword.The throws keyword can be used to declare multiple exceptions, separated by a comma.
The throw keyword is usually used to throw unchecked exceptionsThe throws keyword is usually used to declare the checked exceptions.

As you can see, from the above table of differences, we can see and understand the differences between the throw and throws keywords.

Conclusion

In this article, we have seen the throw and throws keyword, and we have also seen some points of differences related to the throw and throws keyword. We have also seen some examples to understand the throw and throws keyword in Java.

FAQ About Throw and Throws in Java

Q: Why do we use the throw keyword?

Ans: The throw keyword is used in situations when we are required to throw some exception explicitly.

Q: How can we handle exceptions in java?

Ans: In order to handle the exceptions, we can use the try-catch blocks. You can explore more about the try-catch blocks, and understand how they work.

Q: What does the throws keyword do?

Ans: The throw keyword helps us declare that this particular method might throw some exceptions.