C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » Java programming language

Java - Difference between throw and throws

Here, we will learn about throw and throws and what are the core concepts of throw and throws and what are the differences between throw and throws, Compare throw and throws.
Submitted by Preeti Jain, on January 31, 2018

1) throw in java

  1. throw is a keyword introduced in java for exception handling.
  2. Sometimes we can create exception object explicitly and we can handover to the jvm manually for these we have to use throw keyword.
  3. Purpose of throw keyword is to handover our created exception objects to the jvm manually.

Example 1: With the help of throw keyword

class ThrowExample
{
	public static void main(String[] args){
		System.out.println(10/0);
		throw new ArithmeticException("/ by zero");
	}
}

Output

D:\Java Articles>java ThrowExample
Exception in thread "main" java.lang.ArithmeticException: / by zero
        at ThrowExample.main(ThrowExample.java:6)

Example 2: Without the help of throw keyword

class ThrowExample
{
	public static void main(String[] args){
		System.out.println(10/0);
	}
}

Output

D:\Java Articles>java WithoutThrow
Exception in thread "main" java.lang.ArithmeticException: / by zero
        at WithoutThrow.main(WithoutThrow.java:6)
  • In Example 1, Developer is responsible to create exception object explicitly and handover object to the jvm manually (i.e. here main method is not responsible to create object and handover to jvm internally). We will get the same output in both ways and the methodologies are different.
  • In Example 2, main() method is responsible to create exception object explicitly and handover object to the jvm internally (i.e. here, programmer is not responsible to create object and handover to jvm manually). We will get the same output in both ways and the methodology are different.
  • throw keyword is used to throw an exception from any method or static block in java.
  • Best use of throw keyword is for user defined or customized exception.
  • throw keyword is applicable only for Throwable objects.

2) throws in java

  1. throws is a keyword introduced in java for exception handling.
  2. In our program, if there is a possibilities of raising checked exception then compulsory we should handle the checked exception otherwise we will get compile time error (ie. unreported exception xxx(exception name in place xxx) must be caught or declared to be thrown).

Example 1:

import java.io.*;
class ThrowsExample{
	public static void main(String[] args){
		PrintWriter  pw = new PrintWriter("java.txt");
		pw.println("welcome in java world");
	}
}

Output

D:\Java Articles>javac ThrowsExample.java
ThrowsExample.java:6: error: unreported exception FileNotFoundException; must be
 caught or declared to be thrown
        PrintWriter pw = new PrintWriter("java.txt");
                         ^
1 error

We can use throws keyword to delegate the responsibility to the caller method (i.e. it may be another method or jvm) then caller method is responsible to handle that exception.

Example 2:

class ThrowsExample2 {
	public static void main(String[] args) throws  InterruptedException{
		Thread.sleep(1000);
	}
}
  • throws keyword is applicable only for checked exceptions otherwise we will get compile time error (ie. unreported exception).
  • Usage of throws keyword for unchecked exception is meaningless you can use if you want but no compile time error we will get (no impact on unchecked exception).
  • throws keyword does not prevent abnormal termination of the program.


Liked this article? Do share with your friends :)


COMMENTS