Sam
Sam

Reputation: 365

Avoid same try catch blocks in Java

I am developing a project which involves JSON manipulation in Java using JSON API. There are many places where I need to read values from JSON file. The API provides checked exceptions for the same. Everytime I use the API to read JSON values, I am forced to write try catch block. As a result, there is a large number of try catch blocks. It makes the code look messy.

    String Content = "";
    try {
        read = new BufferedReader(new FileReader("Data.json"));
    }
    catch(Exception e) {
        System.out.println("File Not found");
    }

    try {
        while((line = read.readLine() ) != null) { 
            Content = Content+line;     
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        ResponseArr = new JSONArray( Content );
    } catch (JSONException e) {
        e.printStackTrace();
    }
    try {
        ResponseObj = ResponseArr.getJSONObject(1).getJSONArray("childrens");

    } catch (JSONException e) {
        e.printStackTrace();
    }
    try {
        StoreResponse = ResponseArr.getJSONObject(0).getJSONArray("childrens");

    } catch (JSONException e) {
        e.printStackTrace();
    }

Is there any way to avoid this ?A single try catch block would not suffice and the statements are not dependent. Each read statement requires a separate try catch block as I have to log the details of places while catching the exception. Can I invoke a common method whenever I have a code to read JSON data, like sending the code as a paramater to a method which would take care of the exception handling or some other way round ?

Upvotes: 1

Views: 865

Answers (3)

Fontanka16
Fontanka16

Reputation: 1323

Since (all?) the subsequent statements are dependent on the previous it makes no sense having that many try/catch blocks. I would rather put the code inside one try/catch and handle the exceptions by type

Pseudo-code:

 String Content = "";
    try {
        read = new BufferedReader(new FileReader("Data.json"));
        while((line = read.readLine() ) != null) { 
            Content = Content+line;     
        }
        ResponseArr = new JSONArray( Content );
        ResponseObj = ResponseArr.getJSONObject(1).getJSONArray("childrens");
    } catch (JSONException e) {       
        e.printStackTrace();    
    } catch(FileNotFoundException)
            System.out.println("File Not found");
    }
    // and so on

As some are suggesting, you might want to let all these exceptions bubble up (not catching them) since you're not doing anything meaningful when catching them. However, I think that depends on the calling context.

Upvotes: 1

Abdul Manaf
Abdul Manaf

Reputation: 5003

Try like this

String Content = "";
try {
    read = new BufferedReader(new FileReader("Data.json"));
       while((line = read.readLine() ) != null) { 
        Content = Content+line;     
      }
      ResponseArr = new JSONArray( Content );
      ResponseObj = ResponseArr.getJSONObject(1).getJSONArray("childrens");
    } 
    catch (IOException e) {
      e.printStackTrace();
    }
    catch (JSONException e) {
      e.printStackTrace();
    }
    catch(Exception e) {
      System.out.println("File Not found");
    }

Upvotes: 0

Dev.H
Dev.H

Reputation: 1

If you are handling all exceptions in the same way, why not combine them in one try/ catch clause for example like this :

try {
        while((line = read.readLine() ) != null) { 
            Content = Content+line;     
        }
       ResponseArr = new JSONArray( Content );
       ResponseObj = ResponseArr.getJSONObject(1).getJSONArray("childrens");
    } catch (Exception e) {
        e.printStackTrace();
    }

Upvotes: 0

Related Questions