0

What is the difference between having 2 function calls in one "try" and having them separated in continuous "try" cases?

I tend to only have one thing inside, but I have seen many codes having 3, 10, 20, ... lines of code inside a single "try". This doesn't make sense to me because if there is a potential risk in a specific line of code (e.g. function call) I would focus on that line meaning that putting the "try" only around this very line and not more.

So what do the experts say?

// Code ref #: 1
try
{
    baz();
    qux();
} 
...

or would you do as following?

// Code ref #: 2
try
{
    baz();
} 
...

try
{
    qux();
} 
...    

Ok, I guess this actually depends on the considered handling, i.e. if I want to handle any error the same way or in different ways!?

Thx a lot!

RadioMan85
  • 337
  • 1
  • 11
  • 1
    See if https://stackoverflow.com/questions/2737328/why-should-i-not-wrap-every-block-in-try-catch?rq=1 answers your question – D Stanley Mar 30 '20 at 14:40
  • 1
    ... usually 42 ... – Selvin Mar 30 '20 at 14:40
  • 3
    This is a very general question, there is no answer to your question because it depends on a lot of factors. Does the code in the `try` clause depend on what method threw? Can you know what method threw by the type of the exception? Do you actually need to handle the excpetion at all? If there is a general rule, it should be that everything inside a try should be logically connected, that is, if something fails at some point, then everything else inside the try should be ensuing steps that must not be executed. This could mean a couple of lines of code, a thousand or the whole application... – InBetween Mar 30 '20 at 14:44
  • but seriously it depends what if `qux()` depends on `baz()` call ? then your 2nd code doesn't make sens ... it would have more sens if `try` would be nested ... what if quax doesn't throw and it depends on baz which throws ? then obviously 1st code is better choice – Selvin Mar 30 '20 at 14:44
  • @D Stanley: Thx!! – RadioMan85 Mar 30 '20 at 14:49
  • @InBetween: Well you hit it. That was obviously a general question! And your answer "is" an answer on my question. So thank you ;-) It provids me the questions I was out for. – RadioMan85 Mar 30 '20 at 14:57
  • @Selvin: Thx, now it starts to make sense in my head. – RadioMan85 Mar 30 '20 at 15:01
  • I'm glad you found it useful! – InBetween Mar 30 '20 at 15:02

1 Answers1

1

Asking how many statements you should put in a try is like asking how many statements you should put in an if, or a for loop. Why did you never ask "how many statements should I put in an if statement?"? Because you understand what an if statement is, and that the number of statements in it doesn't matter*. What matters is "what statements make sense when put in there?" So instead, you'd probably asked "What do I put inside an if?".

The above paragraph stopped making sense after OP's edit of the question, but that's fine!

What you seem to not understand is why we sometimes put many lines of code inside a try. Well, one example of this is when you read a JSON file. You'd do something like this (very rough example, for illustration purposes only):

try {
    string json = ReadFile();
    MyObject obj = ParseJSON(json);
    DoSomethingWithMyObject(obj);
} catch (FileNotFoundException ex) {
    ShowErrorToUser("File Not Found");
} catch (JsonReaderException ex) {
    ShowErrorToUser("Invalid File");
}

ReadFile might throw a FileNotFoundException and ParseJSON might throw a JsonReaderException. You can't really separate these three lines into 2 trys:

string json;
MyObject obj;
try {
    json = ReadFile();
} catch (FileNotFoundException ex) {
    ShowErrorToUser("File Not Found");
}
try {
    obj = ParseJSON(json);
} catch (JsonReaderException ex) {
    ShowErrorToUser("Invalid File");
}
DoSomethingWithMyObject(obj);

Why? Because definite assignment rules says that json and obj are not definitely assigned when they are used! You see, even when ReadFile fails, ParseJSON will still execute, but json will not have a value if ReadFile fails!

If you say "well, I can just give json a default value then!", that's true, but it doesn't always make sense to do so. Sometimes one part of your method just can't run if another part of it throws an exception. This is a sign that the two parts should be in the same try block#.


*Well, it does matter if you're talking about style...

#Obviously, if your method can't handle the exception, then there should not be a try block at all.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • Thank you for your input! What I didn't thought about is, that (referring to my examples) function "qux()" will not be executed if "baz()" fails. Now its obvious. And the obviousness might be the reason why I didn't found an answer else where :-/ Sorry for the badly formulated question in the first round. However the answers I was looking for came across right away. – RadioMan85 Mar 30 '20 at 18:48