I do not think you can say that one of them is better. There is just another balance of pros and cons.
The main advantage of the two-block approach is that you get the best separation of code for a โhappyโ path and code for error management. (This separation is similar to one of the advantages provided by using exceptions, but it is another beast, because catch blocks allow you to collect in one place, that is, outside your "functional" block, all the code to control a bunch of possible error conditions that may arise inside " functional "block and the control of which will usually be scattered throughout, in the above example with two blocks there is nothing like this, since the code for controlling the error condition still seems mixed with the rest of the code of your fun tion).
On the other hand, it is quite possible that in both cases, i.e. successes and failures, you would like to take some common action. Consider, for example, serializing a sequence of network operations: when one operation completes, you perform the next operation, both during the first successful operation and in the event of a failure. This is certainly the case when you will have some code replication if you use a two-block approach.
In general, I donโt think that there is a big difference, since you can easily do what you need to do with both approaches, but in specific cases one approach may fit your workflow better than another.
Only my 2 cents.
source share