Java Try Catch Block Size

This may be a strange question, but I still thought that I would ask you to get an idea about this and not to let me do something wrong while I was coding.

Let's say I have a func1 () function inside which I call func2 (). The func2 () function throws an exception e1. And I want to catch this exception inside func1 ().

Does it matter whether I start the try block at the beginning of the function itself and end it at the end of func1 () with the catch block, and not just surround the part of the code where I call the func2 () function.

I know from the point of view of coders where, if an exception occurs, he will be able to know exactly where the exception came from. If we ignore this, are there any other negative consequences of just putting the whole method inside try-catch?

Edit - code snippet

So, I am converting a JSON string to a JSON Node. This operation throws an exception. But instead of surrounding this single statement with a try-catch block, I put the whole function inside a try block. It just looks cleaner to me. :)

public void storePublicData(String publicData, String twitterId) {

    try {

        Date date=new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String day = formatter.format(date);

        BasicDBObject query = new BasicDBObject("date", day);
        query.append("brand_id", twitterId);

        JsonNode publicDataJsonNode;

        publicDataJsonNode = JSONOperations.castFromStringToJSONNode(publicData);


        DBObject document = BasicDBObjectBuilder.start()
                .add("brand_id", twitterId)
                .add("date", day)
                .add("followers", publicDataJsonNode.get("followersCount").asText())
                .add("tweets", publicDataJsonNode.get("tweetsCount").asText())
                .get();

        twitterCollection.update(query,new BasicDBObject("$set", document), true, false);

    } catch (JSONParamsException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
+4
source share
5 answers

The biggest drawback is that you can also catch an exception that you do not intend to catch.

For example, let's say you have a method that can call NullPointerException, and you can handle this case. (This method is probably poorly written, but let me say that this is a library method, and you cannot change it). So you will catch NPE:

void func1() {
    try {
        func2();
        if (someString.equals("some value") {
            someOtherFunction();
        }
    } catch (NullPointerException e) {
        // handle func2() NPE somehow
    }

}

, NPE try: func2 someString.equals, someString - null. , , , .

, ( , try-catch, ..) , , .

+3

, try/catch, , , , . , , "" - , IO, int i = 2 + 2;

+2

, , , ,

: , . , , . - (, , ). , , , 2 try/catch - .

+2

, - funct1 funct2, , try-catch.

0

im " ":

- , . , , , . ( ), try , catch/finally.

, , :

public class Test {


  public void doSomthing(){

    // here i don't need to manage the exception because i have built parseDate(String date)
    Date date = parseDate("10-17-2016");

  }


  private Date parseDate(String date){
    Date result = null;
    try {
        result = new SimpleDateFormat().parse(date);//Throws Parse Exception
    } catch (ParseException e) {
        // Here you can:
        //1) Throws other exception (Checked or Unchecked)
        //2) Log the exception
        // I think you should not re-throws the exception because
        //is responsibility of this methods manage the exception
        e.printStackTrace();
    } 
    return result;
  }

}
0

Source: https://habr.com/ru/post/1545429/


All Articles