Exception Handling in JAVA 7
Java throw keyword
The Java throw keyword is used to explicitly throw an exception.Only object of Throwable class or its sub classes can be thrown. Program execution stops on encountering throw statement, and the closest catch statement is checked for matching type of exception.
Syntax:
throw ThrowableInstance
Creating Instance of Throwable class:
There are two possible ways to get an instance of class Throwable,
1. Using a parameter in catch block..
2. Creating instance with new operator.
5. Only the object of Throwable class or its subclasses can be thrown.
new NullPointerException("test");
Example demonstrating throw Keyword
class HelloWorld { static void validate(int age) { if(age < 18) { try { throw new ArithmeticException("NOT Valid"); } catch(ArithmeticException e) { System.out.println("Age Invalid Exception Caught"); } } else { System.out.println("Welcome to vote"); } } public static void main(String args[]) { validate(13); } } Output : Age Invalid Exception Caught
In the above example the validate() method throw an instance of ArithmeticException, which is successfully handled using the catch statement.
Java throws keyword
Any method capable of causing exceptions must list all the exceptions possible during its execution, so that anyone calling that method gets a prior knowledge about which exceptions to handle. A method can do so by using the throws keyword.
Syntax :
return_type method_name ( parameter_list ) throws exception_list
{
//definition of method
}
NOTE : It is necessary for CheckedExceptions (Compile-time) and not necessary for RuntimeException (Run-time) and Error, or any of their subclass.
Java throws example
Let's see the example of java throws clause which describes that checked exceptions can be propagated by throws keyword.
There are two cases:
1. Case 1 : You caught the exception i.e. handle the exception using try/catch.
2. Case 2 : You declare the exception i.e. specifying throws with the method.
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.
class test{ void method()throws IOException{ throw new IOException("device error"); //checked exception } } public class Testthrows{ public static void main(String args[]){ try{ M m=new M(); m.method(); }catch(Exception e){System.out.println("Exception handled");} } } Output: Exception handled
Case2: You declare the exception
A) In case you declare the exception, if exception does not occur, the code will be executed fine.
B) In case you declare the exception if exception occures, an exception will be thrown at runtime because throws does not handle the exception.
class test{ void method()throws IOException{ System.out.println("Device Operation Performed"); } } class Testthrows{ public static void main(String args[]) throws IOException{ //declare exception M m=new M(); m.method(); } } Output : A) Device Operation Performed B) Runtime Exception
NOTE : If you are calling a method that declares an exception, you must either caught or declare the exception.
Difference between throw and throws in Java:
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. | Throws is followed by class. |
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. |
No comments:
Post a Comment
Hai , Post your comment . (required, Bugs, Errors )