The Java throws keyword is used to declare an exception and the main objective of throws is used to delegate the responsibility of the exception handling to the caller i.e. other method or JVM then caller method is responsible for handling that exception.
It is declared with method signature and we can declare the multiple exceptions by using throws keyword.
public void writeFile() throws FileNotFoundException, IOException
If there is a chance to occur checked exception in our program then we should handle that exception for smooth execution at runtime otherwise compiler won’t compile the program and it will give the compile time error.
In this program we doesn’t handle any exception, let’s we check how it will work
package com.exceptions;
import java.io.PrintWriter;
public class Test{
public static void main(String[] args) {
PrintWriter pw=new PrintWriter("abc.txt");
pw.println("Hello world");
}
}
in the above program, the compiler won’t compile the program because there is a chance to occur checked exception.
It gives compile time error as
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Unhandled exception type FileNotFoundException
at com.exceptions.Test.main(Test.java:5)
compiler notified to programmer there is a unhandled exception.
We can declare checked exceptions using throws because to convince the compiler and we can declare unchecked exceptions also but no use at all because unchecked exception occurs at runtime.
Handling exception by using throws
We can handle exceptions using try-catch and throws.
package com.exceptions;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
public class Test{
public void writeFile() throws FileNotFoundException{
PrintWriter pw=new PrintWriter("abc.txt");
pw.println("Hello world");
pw.close();
}
public static void main(String[] args) throws FileNotFoundException {
Test obj=new Test();
obj.writeFile();
}
}
In this program we created a file name with “abc.txt” in writeFile() method, we declared FileNotFoundException using throws because there is a chance to occur compile time error.
The writeFile() method is called by the main() method, so the main() method is responsible for handling that exception.
The main() method also declare exception using throws. Finally, JVM will handle that exception because the caller of the main() method is JVM. This termination is abnormal termination.
Few important cases using throws
Case-1:
We can use throws keyword for methods and constructors but not classes
public class Test{
Test()throws Exception{
}
public void writeFile() throws Exception{
}
}
Case-2:
We declare only throwable types only by using throws keyword.
Here Test class is throwable type
public class Test extends Exception{
public void writeFile() throws Test{
}
}
Here Test class is normal class, not throwable type
public class Test {
public void writeFile() throws Test{
}
}
Case-3:
If there no chance of occurring exception in try then no need to handle that exception
public class Test {
public static void main(String[] args) {
try {
System.out.println("hello");
} catch (IOException e) {
System.out.println(e);
}
}
}
We handled the exception in catch block even though in the try block there no chance of occurring exception then we will get compile time error.
This only applicable for fully checked exceptions.
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Unreachable catch block for IOException. This exception is never thrown from the try statement body
at com.exceptions.Test.main(Test.java:10)
No comments:
Post a Comment