Table of contents
1.
Introduction
2.
What is Throw Keyword in Java?
2.1.
Syntax of Throw in Java:
2.2.
Example of Throw in Java:
2.3.
Java
3.
What is Throws Keyword in Java?
3.1.
Syntax of Throws in Java:
3.2.
Example of Throws in Java:
3.3.
Java
4.
Difference Between Throw and Throws in Java
5.
Frequently Asked Questions
5.1.
What are the keywords used in exception handling?
5.2.
What is the difference between throw throws and throwable?
5.3.
What are the Benefits of Java Exception Handling?
5.4.
When to use throws in Java?
6.
Conclusion
Last Updated: Sep 19, 2024
Easy

Difference Between Throw and Throws in Java

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

An exception is an event that happens during the execution of a program that hampers the overall execution of the code. Sometimes, the system handles the exception by default, but in some cases, we need to explicitly handle exceptions based on our code or situations.

Java exception handling is managed via five keywords: try, catch, throw, throws, and finally. The brief description of throw and throws in Java Exception Handling keywords are as follows.

Difference between Throw And Throws in Java

Though they look similar but these two keywords are very different:

  • Throws is used to indicate that an exception can be thrown by a function during execution.
  • Throw can be used to throw an exception explicitly within a function or within a block of code.
     

Let's deep dive into the topics one by one and discuss them in detail.

Must Read, Multithreading in javaDuck Number in Java

What is Throw Keyword in Java?

Throw in Java is used to explicitly throw an exception from a method or constructor. We can throw either checked or unchecked exceptions in java by throw keyword. The "throw" key-word is mainly used to throw a custom exception. The only object of the throwable class or its subclasses can be thrown. When a throw statement is encountered, program execution is halted, and the nearest catch statement is searched for a matching kind of exception.

Syntax of Throw in Java:

throw new ArithmeticException("Arithmetic Exception");
void myMethod() {
	try {
		//throwing arithmetic exception using throw
		throw new ArithmeticException("Something went wrong!!");
	} catch (Exception exp) {
		System.out.println("Error: " + exp.getMessage());
	}
}

Example of Throw in Java:

  • Java

Java

public class Main {
public static void validateMarks(int marks) {
if (marks<80)
throw new ArithmeticException("Not Oracle certified");
else
System.out.println("Oracle certified ");
}
public static void main(String args[]) {
validateMarks(86);
System.out.println("welcome ...");
}
}
You can also try this code with Online Java Compiler
Run Code

Output:

Explanation: 

In this example, we have created the validateMarks() method that takes an integer value as a parameter. If the marks is less than 80, we are throwing the ArithmeticException otherwise, print a message that you are Oracle certified. 
 

Also see,  Swap Function in Java

What is Throws Keyword in Java?

Methods may throw exceptions during the execution of the program using the throws keyword. If a method is able to cause exceptions is called, it must list all the exceptions that might occur during execution. This is so that the caller knows how to handle the exceptions. To do this, a method should use the throws keyword.

Syntax of Throws in Java:

throws ArithmeticException;
//Declaring arithmetic exception using throws
void sample() throws ArithmeticException{
   //Statements
}

Example of Throws in Java:

  • Java

Java

public class Main {
   public static void check() throws ArithmeticException {
       System.out.println("Inside check function");
       throw new ArithmeticException("demo");
   }
   public static void main(String args[]) {
       try {
           check();
       } catch (ArithmeticException e) {
           System.out.println(e);
       }
   }
}
You can also try this code with Online Java Compiler
Run Code

Output:

Try it on online java compiler.


Explanation: 

In this example, the programmer makes a prior announcement that the code may be risky, so throws exception with the method itself. The above codes will throw an exception if the file as mentioned above is not created first or gets deleted. 

Difference Between Throw and Throws in Java

Parameters

Throw

Throws

Locate throw keyword are always present inside the method body. throws keyword always used with method signature.
Number of exception We can throw only one exception at a time. We can handle multiple exceptions using the throws keyword.
Followed by throw is followed by an instance. throws is followed by class.
Syntax

Syntax: void main() throw new ArithmeticException{

}

Syntax: void main() throws ArithmeticException {

}

Uses throw key-word is used to explicitly throw an exception. throws key-word is used to declare an exception.

Also see, Hashcode Method in Java

Frequently Asked Questions

What are the keywords used in exception handling?

There are five keywords used in Java exception. catch, try, throw, throws and finally

What is the difference between throw throws and throwable?

In Java, throw is used to explicitly throw an exception. throws declares that a method can throw specified exceptions. Throwable is the superclass for all errors and exceptions, encompassing both Exception and Error classes.

What are the Benefits of Java Exception Handling?

  • Separating Error-Handling code from regular business logic code
  • Grouping and differentiating error types
  • Propagating errors up the call stack

When to use throws in Java?

Use the throws keyword in Java to indicate that a method can potentially throw an exception. It's required when dealing with checked exceptions that the method does not handle internally, thereby making exception handling mandatory for the caller.

Conclusion

In this blog, we have discussed the difference between Throw and Throws in Java. Understanding the distinction between throw and throws is crucial for effective Java exception handling. throw is used to manually trigger exceptions, while throws indicate potential exceptions a method might encounter.

Recommended Reading: 

Practice makes a man perfect. To practice and improve yourself in the interview, you can check out Top 100 SQL problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews.

Live masterclass