Saturday, 10 January 2015

What is throws keyword in Java

throws keyword:

The 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 of the code can be maintained. Exception Handling is mainly used to handle the checked exceptions. If there occurs any unchecked exception such as NullPointerException,ArithmeticException  it is programmers fault so  we  can't  check up before the code being used.

Syntax of throws keyword:

void method_name() throws exception_class_name {
...
}

Which type of  exception should we declare ?

checked exception only, because:
unchecked Exception: under programmer control so programmer can correct the code.
error: beyond your control e.g. you are unable to do anything if there occurs VirtualMachineError or
StackOverflowError.
checked exception: depends on other vendors such as  SQLException,  IOException, FileNotFoundException etc.

Advantage of throws keyword:

Now Checked Exception can be propagated (forwarded in call stack of different methods).

Example which show how checked exceptions can be propagated by throws keyword.

In this example,we are going to see that checked exception also propagates if method declares an exception (i.e. specifies throws keyword with the exception class).

import java.io.IOException;

class Test{
void getFile() throws IOException{
throw new IOException("IO Exception may occur"); //checked exception
}
void display() throws IOException{
getFile();
}
void show(){
try{
display();
}catch(Exception e) { System.out.println("exception handled"); }
}
public static void main(String []args){
Test t1=new Test();
t1.show();
System.out.println("normal flow of the program");
 }
}

Output: exception handled
normal flow of the program

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

There may be two cases of that:
>1. You caught the exception i.e. handle the exception by try/catch block.
>2. You declare the exception i.e. declare the exception by throws with the method.

>1. You handle the exception:

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

import java.io.*;

class Student{
void getFile() throws IOException{
throw new IOException("IO exception may occur");
}
}
class Test{
public static void main(String []args){
try{
Student st=new Student();
st.getFile();
}
catch(Exception e) { System.out.println("exception handled"); }
System.out.println("normal flow of  code");
 }
}

Output: exception handled
normal flow of code

>2. You declare the exception:

In this case there are two case.
1. In case you declare the exception, if exception does not occur, the code will be executed fine.
2. In case you declare the exception if exception occures, an exception will be thrown at runtime because throws does not handle the exception.

1. Example of program  declaring the exception if exception does not occur in the method which declares an exception:

import java.io.*;

class Student{
void display()throws IOException{
System.out.println("IO Exception may occur");
}
}
class Test{
public static void main(String []args)throws IOException {  //declare exception here
Student st=new Student();
st.display();
System.out.println("normal flow of code");
}
}

Output: IO exception may occur
normal flow of  code

2. Example of program declaring the exception if exception occurs in the method which declares an exception:

import java.io.*;

class Student{
void display()throws IOException{
throw new IOException("IO exception may occur");
}
}
class Test{
public static void main(String []args)throws IOException  { //declare exception here
Student st=new Student();
st.display();
System.out.println("normal flow of code");
}
}

Output: Exception in thread "main" java.io.IOException: IO exception may occur

Can we rethrow an exception ?

Yes, by throwing same exception in catch block.


What is the difference between throw and throws keyword:

                     throw                                                                   throws
1. throw is used to explicitly throw an exception.      1. throws is used to declare an exception.
2. checked exception can not be propagated .            2. checked exception can be propagated                        without throws.                                                           with throws.
3. throw is followed by an instance.                           3. throws is followed by class.
4. throw is used within the method.                           4.  throws is used with the method signature.
5. You cannot throw multiple exception                    5. You can declare multiple exception e.g.
                                                                                         public void method()throws
                                                                                         IOException,SQLException.

Next>> Exception Handling with Method Overriding



No comments:

Post a Comment