throws in Java

throws is a keyword in Java. Which is used to throw the exception to super class if we don’t handle it in the current class. throws will be declared to method which may produce the exception. It is declared to method after parenthesis closed before curly braces open.

Syntax: void test() throws IOException{}

Different Scenarios:

Case 1: If a method throws UnChecked Exception then then for calling method not mandatory to declare throws.

Example:

package com.gudla.exceptionhandle;

public class ExceptionB{
    void test() throws ArrayIndexOutOfBoundsException{
        System.out.println("I am Exception B");
    } 
}

package com.gudla.exceptionhandle;

public class ExceptionHandleTest {
    public static void main(String[] args){
        ExceptionB a = new ExceptionB();
        a.test();
    }
}

Output:

I am Exception B

Case 2: If a method is overloaded and one method is declared with Unchecked exception then other method not mandatory to declare throws.

Example:

package com.gudla.exceptionhandle;

public class ExceptionB{
    void test(){
        System.out.println("I am Exception B");
    }
    void test(int i) throws ArrayIndexOutOfBoundsException {
        System.out.println("I am Exception B "+ i);
    }
}

package com.gudla.exceptionhandle;

public class ExceptionHandleTest {
    public static void main(String[] args){
        ExceptionB a = new ExceptionB();
        a.test();
        a.test(8);
    }
}

Output:

I am Exception B
I am Exception B 8

Case 3: If a method throws a checked exception then Calling method also should declared with throws. Here calling method can declared with specific exception which throws called method or its super class exception. But reverse is not possible.

Example:

package com.gudla.exceptionhandle;

import java.io.IOException;

public class ExceptionB{
    void test()  throws IOException {
        System.out.println("I am Exception B");
    }
}

package com.gudla.exceptionhandle;

public class ExceptionHandleTest {
    public static void main(String[] args) throws Exception{
        ExceptionB a = new ExceptionB();
        a.test();
    }
}

Output:

I am Exception B

Case 4: If a method is overloaded and one method is declared with Checked Exception then other method may not need to declared with throws.

Example:

package com.gudla.exceptionhandle;
import java.io.IOException;

public class ExceptionB{
    void test()  throws IOException {
        System.out.println("I am Exception B");
    }
    void test(int i){
        System.out.println("I am Exception B "+ i);
    }
}

package com.gudla.exceptionhandle;
import java.io.IOException;

public class ExceptionHandleTest {
    public static void main(String[] args) throws IOException{
        ExceptionB a = new ExceptionB();
        a.test();
        a.test(8);
    }
}

Output:

I am Exception B
I am Exception B 8

Case 5: If calling method declared with throws exception then its not mandatory to declare throws for called method.

Example:

package com.gudla.exceptionhandle;

public class ExceptionB{
    void test(){
        System.out.println("I am Exception B");
    }
}


package com.gudla.exceptionhandle;
import java.io.IOException;

public class ExceptionHandleTest {
    public static void main(String[] args) throws IOException{
        ExceptionB a = new ExceptionB();
        a.test();
    }
}

Case 6: When a method is overridden and super class method is declared with throws unchecked exception then sub class method or calling method may not required to declare throws exception. And also if sub class method declared with throws unchecked exception then super class method or method may not required to throws exception.

Example:

package com.gudla.exceptionhandle;

public class ExceptionA {
    void test() throws ArrayIndexOutOfBoundsException{
        System.out.println("I am Exception A");
    }
}

package com.gudla.exceptionhandle;

public class ExceptionB extends ExceptionA{
    void test(){
        System.out.println("I am Exception B");
    }
}

package com.gudla.exceptionhandle;

public class ExceptionHandleTest {
    public static void main(String[] args){
        ExceptionA a = new ExceptionB();
        a.test();
    }
}

Output:

I am Exception B

Case 7: When a method is overridden and super class method is declared with throws checked exception then subclass method not mandatory to declare exception but calling method should declare with throws exception(which may be same exception as super class method or its exception super class).

Example:

package com.gudla.exceptionhandle;
import java.io.IOException;

public class ExceptionA {
    void test() throws IOException{
        System.out.println("I am Exception A");
    }
}

package com.gudla.exceptionhandle;

public class ExceptionB extends ExceptionA{
    void test(){
        System.out.println("I am Exception B");
    }
}

package com.gudla.exceptionhandle;

public class ExceptionHandleTest {
    public static void main(String[] args) throws Exception{
        ExceptionA a = new ExceptionB();
        a.test();
    }
}

Output:

I am Exception B

Case 8: When a method is overridden and sub class method is declared with throws checked exception then its super class method and calling method should throws exception(which may be same exception as subclass method or its exception super class).

Example:

package com.gudla.exceptionhandle;

public class ExceptionA {
    void test() throws Exception{
        System.out.println("I am Exception A");
    }
}

package com.gudla.exceptionhandle;

import java.io.IOException;

public class ExceptionB extends ExceptionA{
    void test() throws IOException{
        System.out.println("I am Exception B");
    }
}

package com.gudla.exceptionhandle;

public class ExceptionHandleTest {
    public static void main(String[] args) throws Exception{
        ExceptionA a = new ExceptionB();
        a.test();
    }
}

Output:

I am Exception B

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s