Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How much code to put in try-catch block [closed]

Tags:

java

try-catch

Is there a "best practice" for how much code to put inside a try/catch block?

I have posted 3 different scenarios below.

I did not include behavior in each catch block and i did not include the finally block. This was to improve readability for viewers. Assume each catch does something differently. And assume the finally will be closing the stream. Just trying to create an easy to read example for future readers.

  1. Control, no try/catch.
  2. Code with 1 try/catch for each place needed.
  3. Code with only 1 try/catch surrounding whole code block.

What is generally accepted as the best practice and why?


Scenario 1

Code without try/catch, just for control.

    BufferedReader bufferedReader = new BufferedReader(new FileReader("somepath"));
    String line;
    while ((line = bufferedReader.readLine()) != null) {
        Object object = new Object();
        this.doSomething(object);
    }
    bufferedReader.close();

Scenario 2

Code with a try/catch block for each individual place needed.

    BufferedReader bufferedReader = null;
    try {
        bufferedReader = new BufferedReader(new FileReader("somepath"));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    String line;
    try {
        while ((line = bufferedReader.readLine()) != null) {
            Object object = new Object();
            this.doSomething(object);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        bufferedReader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

Scenario 3

Code with 1 try/catch surrounding the whole block of code.

    try {
        BufferedReader bufferedReader = new BufferedReader(new FileReader("somepath"));
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            Object object = new Object();
            this.doSomething(object);
        }
        bufferedReader.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
like image 700
prolink007 Avatar asked Oct 24 '13 15:10

prolink007


People also ask

How do you enter a code on try catch block?

Place any code statements that might raise or throw an exception in a try block, and place statements used to handle the exception or exceptions in one or more catch blocks below the try block. Each catch block includes the exception type and can contain additional statements needed to handle that exception type.

Should all code be in a try catch?

I would recommend against this practice. Putting code into try-catch blocks when you know the types of exceptions that can be thrown is one thing. It allows you, as you stated, to gracefully recover and/or alert the user as to the error.

Does code continue after try catch?

First, the code in try {...} is executed. If there were no errors, then catch (err) is ignored: the execution reaches the end of try and goes on, skipping catch . If an error occurs, then the try execution is stopped, and control flows to the beginning of catch (err) .

Is it good practice to write code in catch block?

Only error prone code is within the try block and the handling of that error is written in the catch block. It is used to handle run time exceptions. It is a good practice to write the code in try block which may generate an error , so, that the code doesn't terminate abruptly.


1 Answers

You should scope your try/catches based on the following criteria:

  • do you need to do different things based on where the exception came from?
  • do you need to do different things based on which exception was thrown?
  • what code needs to be skipped (aka is "invalid") when a given exception is thrown?

answering these questions will enable you to determine the appropriate scope for any try/catch block.

like image 84
jtahlborn Avatar answered Sep 25 '22 03:09

jtahlborn