throws keyword in java - rahul00773/JavaConcepts GitHub Wiki

In our program, if there is a possibility of a rising checked exception then compulsory we should handle that checked exception. Otherwise, we will get a compile-time error saying: Unreported Exception XXX must be caught or declared to be thrown

Example1:

import java.io.*;

class Test{

public static void main(String[] args){

PrintWriter pw = new PrintWriter("abc.txt"); pw.println("hello");

}

}

Example2:

class Test{

public static void main(String[] args){

Thread.sleep(10000); } } // Unreported Exception: Java.lang.InterrruptedException must be caught or declared to be thrown.

We can handle this compile-time error by using the following two ways.

  1. By using try-catch block

class Test{

public static void main(String[] args){ try{ Thread.sleep(10000); } catch(InterruptedException e){ } } }

  1. By using throws keyword

We can throws keyword to delegate the responsibility of exception handling to the caller(It may be another method or JVM). Then the caller method is responsible to handle that exception.

Example:

class Test{

public static void main(String[] args) throws InterruptedException{

Thread.sleep(10000); } }

  1. throws keyword required only for checked exceptions. and usage of throws keyword for unchecked exceptions there is no use or there is no impact
  2. throws keyword required only to convince the compiler. And usage of the throws keyword does not prevent abnormal termination of the program.

public class Test{

public static void main(String[] args) throws IntereptedException{

doStuff(); }

public static void doStuff() throws IntereptedException{

doMoreStuff(); }

public static void doMoreStuff() throws IntereptedException{

Thread.sleep(10000); }

}

In the above program, if we remove at least one throws statement then the code won't compile.

Throws Class or Keyword Purpose:

  1. We can use the throws keyword to delegate the responsibility to the caller

  2. throws keyword required only for checked exceptions. and usage of throws keyword for unchecked exceptions there is no use or there is no impact

3.throws keyword required only to convince the compiler. And usage of the throws keyword does not prevent abnormal termination of the program.

Note: It is recommended to use try-catch over throws keyword.

Case1: We can use throws keyword for method and constructors but not for classes.

class Test throws Exception{ // Here it is invalid

Test throws Exception{ }

public void m1() throws Exception{ }

}

Case2: We can use the throws keyword only for throwable types. If we are trying to use for normal java classes then we will get a compile-time error saying incompatible types

class Test{ // Compile Time error: we will get a compile-time error saying incompatible types

public void m1 throws Test{

}

}

class Test extends extends RuntimeException{ // this is valid because class Test is now subclass of RuntimeException

public void m1 throws Test{

}

}

Case3:

class Test{

public static void main(String[] args){

throw new Exception(); }

}

// Compile time error: Unreported Exception: java.lang.Exception must be caught or declared to be thrown

class Test{

public static void main(String[] args){

throw new Error(); }

}

// RunTime Exception: Exception in Thread main java.lang.error at Thread Main

Case4: WithIn the try block if there is no chance of raising an exception we can not write catch for handle and we will get compile time error.

Exception XXX is never thrown in body of corresponding try statement. But this rule is applicable only for fully checked exceptptions.