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 can be maintained.
throws is to be used when you are not using the try catch statement in your code but you know that this particular class is capable of throwing so and so exception(only checked exceptions).
public void method() throws ExceptionType1, ExceptionType2, ....
A method can declare that it throws more than one exception, in which case the exceptions are declared in a list separated by commas.
Rule: If we are calling a method that declares an exception, you must either caught or declae the exception:
There are two cases:
Case1: We caught the exception i.e handle the exception using try/catch.
Case2: we declare the exception i.e specifying throws with the method.
Case1: Example
In case we handle the exception, the code will be executed fine whether exception occurs during the program or not.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class ThrowsKeywordExample {
public void getFile() throws FileNotFoundException {
File file = new File("filename");
FileOutputStream fileOutputStream = new FileOutputStream(file);
//write complete own logic which causes the exception
}
public static void main(String args[]) {
try {
ThrowsKeywordExample example = new
ThrowsKeywordExample();
example.getFile();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Here throws keyword specifies that method will thrown a FileNotFoundException and we handled it using try/catch block.
**************************************************************
Case2: Example:
import java.io.File;
import java.io.FileOutputStream;
public class ThrowsExample {
/**
* @param args
*/
public static void main(String[] args) {
File file = new File("filename");
FileOutputStream fileOutputStream = new FileOutputStream(file);
//write complete own logic which causes the exception
}
}
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 can be maintained.
throws is to be used when you are not using the try catch statement in your code but you know that this particular class is capable of throwing so and so exception(only checked exceptions).
Following is the syntax for throws keyword:
public void method() throws ExceptionType1, ExceptionType2, ....
{
// Method implementation
}
A method can declare that it throws more than one exception, in which case the exceptions are declared in a list separated by commas.
Rule: If we are calling a method that declares an exception, you must either caught or declae the exception:
There are two cases:
Case1: We caught the exception i.e handle the exception using try/catch.
Case2: we declare the exception i.e specifying throws with the method.
Case1: Example
In case we handle the exception, the code will be executed fine whether exception occurs during the program or not.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class ThrowsKeywordExample {
public void getFile() throws FileNotFoundException {
File file = new File("filename");
FileOutputStream fileOutputStream = new FileOutputStream(file);
//write complete own logic which causes the exception
}
public static void main(String args[]) {
try {
ThrowsKeywordExample example = new
ThrowsKeywordExample();
example.getFile();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Here throws keyword specifies that method will thrown a FileNotFoundException and we handled it using try/catch block.
**************************************************************
Case2: Example:
- In case we declare the exception, if exception does not occur, the code will be executed fine.
- In case we declare the exception if exception occurs an exception will be thrown at runtime because throws does not handle the exception.
import java.io.File;
import java.io.FileOutputStream;
public class ThrowsExample {
/**
* @param args
*/
public static void main(String[] args) {
File file = new File("filename");
FileOutputStream fileOutputStream = new FileOutputStream(file);
//write complete own logic which causes the exception
}
}
Here we did not use any exception handling mechanism(no try/catch, no throws keyword). But here we are using FileOutputStream . So it may throws FileNotFoundException. So while typing the above program in IDE it shows warning like "need to add throws keyword or code should be surrounded by try/catch block".
**************************************************************
So see following code after adding the throws keyword:
Here we did not handle the exception. "throws" keyword will thrown exception during run time.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class ThrowsExample1 {
/**
* @param args
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException {
File file = new File("filename");
FileOutputStream fileOutputStream = new FileOutputStream(file);
//write complete own logic which causes the exception
}
}
**************************************************************
Here also we can use "Exception" in throws keyword as below example if we did not know exact exception type. But it is not recommended because programmer will definitely knows about the exception which may occurred.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class ThrowsExample2 {
/**
* @param args
* @throws FileNotFoundException
*/
public static void main(String[] args) throws Exception{
File file = new File("filename");
FileOutputStream fileOutputStream = new FileOutputStream(file);
//write complete own logic which causes the exception
}
}
**************************************************************
One more point is we can throws multiple Exception types as shown below.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class ThrowsExample3 {
/**
* @param args
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException, ArrayIndexOutOfBoundsException, Exception{
File file = new File("filename");
FileOutputStream fileOutputStream = new FileOutputStream(file);
//write complete own logic which causes the exception
}
}
**************************************************************
But following is not accepted. It does not show any error but not recommended.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class ThrowsExample4 {
/**
* @param args
* @throws FileNotFoundException
*/
public static void main(String[] args) throws Exception,
FileNotFoundException, ArrayIndexOutOfBoundsException{
File file = new File("filename");
FileOutputStream fileOutputStream = new FileOutputStream(file);
//write complete own logic which causes the exception
}
}
"Please remember, first need to write sub level exceptions in ascending order for writing multiple exception types in throws keyword".
**************************************************************
So see following code after adding the throws keyword:
Here we did not handle the exception. "throws" keyword will thrown exception during run time.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class ThrowsExample1 {
/**
* @param args
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException {
File file = new File("filename");
FileOutputStream fileOutputStream = new FileOutputStream(file);
//write complete own logic which causes the exception
}
}
**************************************************************
Here also we can use "Exception" in throws keyword as below example if we did not know exact exception type. But it is not recommended because programmer will definitely knows about the exception which may occurred.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class ThrowsExample2 {
/**
* @param args
* @throws FileNotFoundException
*/
public static void main(String[] args) throws Exception{
File file = new File("filename");
FileOutputStream fileOutputStream = new FileOutputStream(file);
//write complete own logic which causes the exception
}
}
**************************************************************
One more point is we can throws multiple Exception types as shown below.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class ThrowsExample3 {
/**
* @param args
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException, ArrayIndexOutOfBoundsException, Exception{
File file = new File("filename");
FileOutputStream fileOutputStream = new FileOutputStream(file);
//write complete own logic which causes the exception
}
}
**************************************************************
But following is not accepted. It does not show any error but not recommended.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class ThrowsExample4 {
/**
* @param args
* @throws FileNotFoundException
*/
public static void main(String[] args) throws Exception,
FileNotFoundException, ArrayIndexOutOfBoundsException{
File file = new File("filename");
FileOutputStream fileOutputStream = new FileOutputStream(file);
//write complete own logic which causes the exception
}
}
"Please remember, first need to write sub level exceptions in ascending order for writing multiple exception types in throws keyword".
No comments:
Post a Comment