Throw
Sometimes we can create exception object explicitly we can hand over to the JVM manually, for this we have to use throw keyword. Thus, the main objective of throw keyword is to hand over our created exception object to the manually.
throw new ArithmeticException("division by zero");
Hence, the result of following two programs are same.
class Test{ public static void main(String[] args){ System.out.print(10/0); } }
Exception in thread "main" java.lang.ArithmeticException: / by zero
at MyClass.main(MyClass.java:4) In this case main method is responsible to create exception object and hand over to the jvm. | class Test{ public static void main(String[] args){ throw new ArithmeticException(“/ divide by zero”); } }
Exception in thread "main" java.lang.ArithmeticException: / by zero
at MyClass.main(MyClass.java:4) in this case programmer creating exception object explicitly and hand over to the JVM manually. |
Note -:best use of throw keyword is user define exception or customized exception.
Case 1-: throw e if e refer null then we will get nullPointerException.
class Test{ static ArithmeticException e=new ArithmeticException(); public static void main(String[] args){ throw e; } } | class Test{ static ArithmeticException e; public static void main(String[] args){ throw e; } } |
Case 2-:we are not allowed to write any statement directly otherwise we will get compile time error saying unreachable statement.
class Test{ public static void main(String[] args){ System.out.print(10/0); System.out.print(“Hello”); } } Run time Exception | class Test{ public static void main(String[] args){ throw new AE(“/ by zero”); System.out.print(“Hello”); } } Compile Time error |
Case 3-:we can use throw keyword only for throwable types if we are trying to use normal java object, we will get compile time error.
class Test{ public static void main(String[] args){ throw new Test(); } } /MyClass.java:4: error: incompatible types: MyClass cannot be converted to Throwable throw new MyClass(); ^ 1 error | class Test extends RuntimeException{ public static void main(String[] args){ throw new Test(); } } Exception in thread "main" MyClass at MyClass.main(MyClass.java:4)
|
Throws
throws -: in our program if there is possibility of raising checked exception then compulsory we should handle checked exception otherwise we will get compile time error saying unreported exception xxx must be caught or declare to be thrown
Example 1-:
import java.io;
class Test{
public static void main(String[] args){
PrintWriter pw=new PrintWriter("abc.txt");
pw.println("Hello");
}
}
compile time error saying unreported exception, java.io.FileNotFoundException must be caught or declare to be thrown
Example 2-:
class Test{
public static void main(String[] args){
Thread.sleep(1000);
}
}
compile time error saying unreported exception, java.lang.IntteruptedException must be caught or declare to be thrown
we can handle this compile time error by using the following two ways .
- try-catch
ex-:
class Test{
public static void main(String[] args){
try{
Thread.sleep(1000);
}catch(InterruptedException e){
}
}
}
- throws-:we can use throws keyword to delegate responsibility of exception to the caller (it may be another method or JVM) then caller method is responsible to handle that exception.
ex-:
class Test{
public static void main(String[] args) throws InterruptedException {
Thread.sleep(1000);
}
}
points-:
- throws keyword required only for checked exception and usage of throws keyword for unchecked exceptions, there is no use are there is no impact.
- throws keyword required to convince compiler and usage of throws keyword doesn't prevent abnormal termination of the program.
ex-:
class HelloWorld {
public static void main(String args[]) throws InterruptedException{
dco();
}
public static void dco() throws InterruptedException
doo();
}
public static void doo() throws InterruptedException{
dooo();
}
public static void dooo() throws InterruptedException{
Thread.sleep(1000);
}
} - In the above program at least one throws keyword, then the code won't compile .
Conclusion of throws class keyword-:
- we can use to delegate responsibility of exception handling to the caller(it may method or JVM)
- it is required to only for checked exception and usage of throws unchecked exception there is no impact.
- It is required only for to convince compiler and usage of throws does not prevent abnormal termination of program.
Note-:It is recommanded to use try-catch over throws keyword.
Case 1-: we can use throws keyword for method and constructor but not for classes.
Ex-:
class HelloWorld {
HelloWorld() throws Exception {
}
public static void DO()throws Exception{
}
public static void main(String args[]){
}
}
Case 2-:we can use throws keyword only for throwable types if we are trying to user for normal Java classes, the will get compile time error saying incommutable types.
class Test{ public static void main(String[] args) throw Test{ System.out.println("Hello, World!"); } } incompatible types: Test cannot be converted to Throwable | class Test extends RuntimeException{ public static void main(String[] args) throw Test{ System.out.println("Hello, World!"); } }
|
Case 3-:
class Test { public static void main(String[] args){ throw new Exception(); } } CE-:unreported exception Exception; must be caught or declared to be thrown | class Test { public static void main(String[] args){ throw new Error(); } } RE-: Exception in thread "main" java.lang.Error |
Case 4-: within the try block there is no chance of raising of exception then we can't write catch block for that exception otherwise we will get compile time error saying Exception : xxx never thrown in body of corresponding try statement. But this rule is applicable for fully checked exception.
class HelloWorld { public static void main(String[] args){ try{ System.out.print("Hello"); }catch(ArithmeticException e){ } } }
| class HelloWorld { public static void main(String[] args){ try{ System.out.print("Hello"); }catch(Exception e){ } } }
|
import java.io.*; class HelloWorld { public static void main(String[] args){ try{ System.out.print("Hello"); }catch(IOException e){ } } } error: exception IOException is never thrown in body of corresponding try statement | class HelloWorld { public static void main(String[] args){ try{ System.out.print("Hello"); }catch(InterruptedException e){ } } } error: exception InterruptedException is never thrown in body of corresponding try statement |
class HelloWorld { public static void main(String[] args){ try{ System.out.print("Hello"); }catch(Error e){ } } }
|
|