/* related posts with thumb nails */

“throws” clause:

If a method is capable of causing an exception that it does not handle, it must specify this behavior (that it has chance of raising exception) so that callers of the method can guard that block against that exception. When throws clause is used try..catch block is not needed. ‘throws’ clause lists the types of exceptions that a method might throw. It is necessary for all checked exceptions when there is exception handling. When a method that has throws clause is called, it must be written in ‘try’ block with appropriate ‘catch’ block; otherwise it should be invoked in a method with throws clause.


type method-name(param-list) throws exception1, exception2,..


….. // code that may raise exception


Example: The following demonstrates the use of throws clause. Here, as, we are calling the readLine() method with a BufferedReader object, we need to catch the IOException. But, we have not handled that exception which may be reported as an error by the system. To avoid this, we have added “throws IOException” to main method.

import java.io.*;

class Test


public static void main(String as[]) throws IOException


InputStreamReader isr=new InputStreamReader(System.in);

BufferedReader br=new BufferedReader(isr);

System.out.println("Enter Name:”);

String n=Integer.parseInt(br.readLine());




Related Topics:


Post a Comment