0

"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();
        }
    }
}
Avi Tyagi
  • 61
  • 3
  • 1
    You don't need a throws-clause if the exception can never be thrown or rethrown. – Meno Hochschild Feb 07 '18 at 16:57
  • If you already handle the exception inside the method with a catch block and don't rethrow it, how would it ever get thrown from the method itself? Short answer: It's bad and redundant. Your `catchExceptionMethod` will never throw a `FileNotFoundException` and adding `throws FileNotFoundException` is just a straight out lie ;). – OH GOD SPIDERS Feb 07 '18 at 16:59
  • Thanks for the response OHGODSPIDERS and MenoHochschild – Avi Tyagi Feb 11 '18 at 17:28

1 Answers1

0

As others already pointed out in their comments, catching an exception (without rethrowing it) and declaring it in the throws clause is nonsense, because the exception will never leave your method.

Most of the time, catching an exception is bad. If your method can't complete its job, throw an exception, so your calling method knows that this step failed and can abort its own computation (you typically get this very useful behaviour by just doing nothing about the exception besides declaring it in the throws clauses of the relevant methods).

Catch an exception only if you know how you can usefully continue even knowing that some previous step failed - and very often, you can't, so surrounding only top-level actions with try-catch, there logging the exception to some log file and informing the user often is the best thing you can do.


The question was asked, what is a top-level action in that context? Typically, it's a block of code that fulfills some task coming from the outside of your program.

So that depends on the type of application. If it's a console application, it's typically the main() method. For a desktop application, top-level might be the menu and button actions. If it's some kind of server, it might be the code implementing some request from the client, and so on.

Generally, it's a place where you can say: "Well, that [e.g. an open-file action] failed, but its success isn't necessary to do the next thing [which might be creating a new file, as chosen by the user]."

Don't catch in the middle of some chain of actions making up your problem-solving algorithm. Most probably the rest of the chain will produce nonsense if some prerequisite step failed.

Ralf Kleberhoff
  • 6,990
  • 1
  • 13
  • 7