The Java throws keyword is used to declare an exception. It gives an information to the programmer that there may occur an exception so it is better for the programmer to provide the exception handling code so that normal flow can be maintained.
Exception Handling is mainly used to handle the checked exceptions. If there occurs any unchecked exception such as NullPointerException, it is programmers fault that he is not performing check up before the code being used.

  • Syntax of throws keyword

       
     returnType methodName() throws ExceptionType
    {  
        //method code  
    }  

    Example)
    drag_handle
     
    //example of taking input from user using BufferedReader
    //it may cause checked exception
    import java.io.*;
    class Example {
        public static void main(String arr[])throws IOException { 
            InputStreamReader isr=new InputStreamReader(System.in);
            BufferedReader br=new BufferedReader(isr);
            System.out.println("Enter your name");
            String name=br.readLine();//reading the input from console
            // as line
            System.out.println("Hello Mr. "+name);
        }        
    }
    Output:
    Enter your name
    Tony    
    Hello Mr. Tony
    
  • Which exception should we declare?

    checked exception only, because:
    unchecked Exception : under your control so correct your code. error : beyond your control e.g. you are unable to do anything if there occurs VirtualMachineError or StackOverflowError.

  • There are two cases:

    Case1 : You caught the exception i.e. handle the exception using try/catch.
    Case2 :You declare the exception i.e. specifying throws with the method.

    • Note : If you are calling a method that declares an exception, you must either caught or declare the exception.

  • Case1: You handle the exception

    In case you handle the exception, the code will be executed fine whether exception occurs during the program or not.

    Example)
    drag_handle
     
    import java.io.*;
    class M{
     void method()throws IOException{
      throw new IOException("device error");
     }
    }
    
    
    class Test{
       public static void main(String args[]){
        try{
         Test t=new Test();
         t.method();
        }catch(Exception e){System.out.println("exception handled");}   
    
        System.out.println("normal flow...");
      }
    }
    Output:
     exception handled
    normal flow...
  • Case 2: You declare the exception

    In case you declare the exception, if exception does not occur, the code will be executed fine. In case you declare the exception if exception occurs, an exception will be thrown at runtime because throws does not handle the exception.


    A)Program if exception does not occur

    
    import java.io.*;
    class M{
     void method()throws IOException{
      System.out.println("device operation performed");
     }
    }
    
    
    class Test{
       public static void main(String args[])throws IOException{//declare exception
        Test t=new Test();
        t.method();   
    
        System.out.println("normal flow...");
      }
    }

    Output:
    Output:
    device operation performed
    normal flow?

    B) Program if exception occurs

    
    import java.io.*;
    class M{
     void method()throws IOException{
      throw new IOException("device error");
     }
    }
    
    
    class Test{
       public static void main(String args[])throws IOException{//declare exception
        Test t=new Test();
        t.method();   
    
        System.out.println("normal flow...");
      }
    }

    Output:
    Runtime Exception
  • Difference between throw and throws

    throw throws
    Java throw keyword is used to explicitly throw an exception. Java throws keyword is used to declare an exception.
    Checked exception cannot be propagated using throw only. Checked exception can be propagated with throws.
    Throw is followed by an instance(Object). Throws is followed by class Type.
    Throw is used within the method. Throws is used with the method signature.
    You cannot throw multiple exceptions. You can declare multiple exceptions e.g. public void method()throws IOException,SQLException.

What next?

The next topic is Exception propagation in java

Share this page