"Using try-catch and throws both for exception handling", is it a good approach ? What are the applicable pros/cons of using this approach ?
Please look at the below sample code snippet, I am having a try-catch block as well as throws clause. In the below code snippet, I am trying read a file in the catchExceptionMethod() which throws a FileNotFoundException as well as catching it in the catch block.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
public class ExceptionTest {
public static void main(String[] args) {
ExceptionTest obj=new ExceptionTest();
try {
obj.catchExceptionMethod("test.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public void catchExceptionMethod(String path) throws FileNotFoundException{
try {
BufferedReader br=new BufferedReader(new FileReader(new File(path)));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}