Java Tutorial |
|||
Produced by Ashish Goel
| |||
Java OOP's Concepts |
|||
Produced by Ashish Goel
| |||
|
|
|||
|
| |||
|
| |||
|
| |||
|
| |||
|
| |||
|
| |||
|
| |||
|
| |||
|
| |||
Java String |
|||
Produced by Ashish Goel
| |||
|
| |||
|
| |||
|
| |||
|
| |||
Exception Handling in Java |
|||
Produced by Ashish Goel
| |||
|
| |||
|
| |||
|
| |||
|
| |||
Java Tutorial |
|||
Produced by Ashish Goel
| |||
Java OOP's Concepts |
|||
Produced by Ashish Goel
| |||
|
|
|||
|
| |||
|
| |||
|
| |||
|
| |||
|
| |||
|
| |||
|
| |||
|
| |||
|
| |||
Java String |
|||
Produced by Ashish Goel
| |||
|
| |||
|
| |||
|
| |||
|
| |||
Exception Handling in Java |
|||
Produced by Ashish Goel
| |||
|
| |||
|
| |||
|
| |||
|
| |||
Output:Exception occured: InvalidAgeException:not validrest of the code...
There are many rules if we talk about methodoverriding with exception handling. The Rules are as follows:
|
Output:Compile Time Error
2) Rule: If the superclass method does not declare an exception, subclass overridden method cannot declare the checked exception but can declare unchecked exception.
import java.io.*;class Parent{void msg(){System.out.println("parent");}}class TestExceptionChild1 extends Parent{void msg()throws ArithmeticException{System.out.println("child");}public static void main(String args[]){Parent p=new TestExceptionChild1();p.msg();}}Output:childIf the superclass method declares an exception
1) Rule: If the superclass method declares an exception, subclass overridden method can declare same, subclass exception or no exception but cannot declare parent exception.
Example in case subclass overridden method declares parent exception
import java.io.*;class Parent{void msg()throws ArithmeticException{System.out.println("parent");}}class TestExceptionChild2 extends Parent{void msg()throws Exception{System.out.println("child");}public static void main(String args[]){Parent p=new TestExceptionChild2();try{p.msg();}catch(Exception e){}}}Output:Compile Time ErrorExample in case subclass overridden method declares same exception
import java.io.*;class Parent{void msg()throws Exception{System.out.println("parent");}}class TestExceptionChild3 extends Parent{void msg()throws Exception{System.out.println("child");}public static void main(String args[]){Parent p=new TestExceptionChild3();try{p.msg();}catch(Exception e){}}}Output:childExample in case subclass overridden method declares subclass exception
import java.io.*;class Parent{void msg()throws Exception{System.out.println("parent");}}class TestExceptionChild4 extends Parent{void msg()throws ArithmeticException{System.out.println("child");}public static void main(String args[]){Parent p=new TestExceptionChild4();try{p.msg();}catch(Exception e){}}}Output:childExample in case subclass overridden method declares no exception
import java.io.*;class Parent{void msg()throws Exception{System.out.println("parent");}}class TestExceptionChild5 extends Parent{void msg(){System.out.println("child");}public static void main(String args[]){Parent p=new TestExceptionChild5();try{p.msg();}catch(Exception e){}}}Output:child
exception handlednormal flow...
Rule: If you are calling a method that declares an exception, you must either caught or declare the exception.
There are two cases:
|
normal flow...
Case2: You declare the exception
import java.io.*;class M{void method()throws IOException{System.out.println("device operation performed");}}class Testthrows3{public static void main(String args[])throws IOException{//declare exceptionM m=new M();m.method();System.out.println("normal flow...");}}
Output:
device operation performednormal flow...B)Program if exception occursimport java.io.*;class M{void method()throws IOException{throw new IOException("device error");}}class Testthrows4{public static void main(String args[])throws IOException{//declare exceptionM m=new M();m.method();System.out.println("normal flow...");}}Output:Runtime ExceptionDifference between throw and throws:
| throw keyword | throws keyword |
|---|---|
| 1)throw is used to explicitly throw an exception. | throws is used to declare an exception. |
| 2)checked exception can not be propagated without throws. | checked exception can be propagated with throws. |
| 3)throw is followed by an instance. | throws is followed by class. |
| 4)throw is used within the method. | throws is used with the method signature. |
| 5)You cannot throw multiple exception | You can declare multiple exception e.g. public void method()throws IOException,SQLException. |