Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
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.
What is throw keyword in Java?
6.
Conclusion
Last Updated: Jun 4, 2024
Easy

Difference between Throw and Throws in Java

Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

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 ...");
}
}

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

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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);
       }
   }
}

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

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

Syntax: void main() throw new ArithmeticException{

}

Syntax: void main() throws ArithmeticException {

}

Usesthrow 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

What is throw keyword in Java?

  • It is used to throw an exception explicitly. 
  • It can either throw checked or unchecked exceptions. 
  • It is used mainly when a custom exception is to be thrown.

Conclusion

In this blog, we have discussed about 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