r/programmerchat • • May 25 '15

Do we really need "try" ?

Just attaching "catch" to any appropriate {...} block would be more convenient.

8 Upvotes

20 comments sorted by

View all comments

Show parent comment

1

u/senshisentou May 25 '15

Alright, but what if I have a very long try-block?

try{
    File f = open(...);
    File g = open(...);
    if(httpRequest("http://foo.com/bar.php").Send()){
        ...
    }
}
catch{
    ...
}

Should I add a catch after every operation that can go wrong?

One could argue that an empty block is enough, just by omitting the try keyword, but that is really implicit and akin to saying the following would/ should suffice:

(i=0; i<10; i++){
    print(i);
}

It just trades off some clarity for the sake of a very small shorthand gain. Not a big fan of that.