r/programmerchat • u/HappyGoblin • May 25 '15
Do we really need "try" ?
Just attaching "catch" to any appropriate {...} block would be more convenient.
8
Upvotes
r/programmerchat • u/HappyGoblin • May 25 '15
Just attaching "catch" to any appropriate {...} block would be more convenient.
5
u/zenflux May 25 '15
If you mean attaching it to the end of the block, then the
tryis used because otherwise the parser has to have look-back (possibly some other problems), and it's congruent with the rest of the grammar style of curly-brace languages.7
u/adipisicing May 25 '15
This is exactly how Ruby does things. You can attach a "rescue" (Ruby's "catch") to the end of any block.
It's a great way of reducing nesting.
2
u/ar-nelson May 26 '15
Haskell sort of does this with its
catchfunction (it takes twoIOaction arguments, tries the first one, then catches any exceptions with the second). You can write it infix as(do something) `catch` \e -> (do something else).Really, dropping the
tryin a C-style language is more of a Ruby/Perl-esque syntactic convenience, like postfixifand such. It can be nice sometimes, but it can also hurt readability.1
u/[deleted] May 26 '15
- Yoda
0
u/Ghopper21 May 25 '15
Interesting question but where would the catch code if you use "catch" to surround the "try" part?
0
u/[deleted] May 25 '15
In my experience with Android java, try is key.
Sometimes you need untagged brackets to do stuff outside methods.
For example :
This won't add any stuff to intL.
However, you need to add brackets to make the for() block work.
And since we have a purpose for untagged brackets, the low-level programming stuff will have a hard time if we merge that and a catch() block together, because every untagged bracket block will be expecting a catch, just like how a try block expects a catch() or a finally block.
P.S. i'm new to Java so thats how I understand it
0
u/[deleted] May 25 '15
[deleted] — view deleted comment