4 ms·
try { // for a really long time } finally { // made a decision }
by calebmpeterson 15y ago
try {
// for a really long time
}
finally {
// made a decision
}
- Locke1689 15y agotry { /* for a really long time */ } finally { /* made a decision */ }
- calebmpeterson 15y agoPlease tell me how you got it formatted like code. I tried a block quote and got italics instead.
- koenigdavidmj 15y agoIndent by four spaces.
- keiferski 15y agoI think two spaces are enough
- zokier 15y agotry { /* thinking for a really long time */ } catch (TimeoutException ex) { /* copy from C# */ }
- vorg 15y agoThere's a JVM language out there that initially allowed try-statements without catch or finally clauses, e.g try{ //for a short time only to decide } But one of its despots, five years after the language was first released, changed the syntax to make a catch or finally clause compulsory, e.g try{ //for a short time to decide }finally{} Because I had used these standalone try-statements a lot, I had to go thru all my code and add empty finally statements to them all. Scala, on the other hand, still allows standalone try-statements. And Scala and C# also have a better closure syntax than that language, and are more worthy to be copied by Java 8.
- ddlatham 15y agoWhat benefits does an empty try block give you? You can always use braces to separate a block without try at all.
- superkarn 15y agoYou can use it to suppress exceptions.
- vorg 15y ago> What benefits does an empty try block give you? An empty try block allows us to repeat variable declarations with the same name in a long stretch of scripty-style code. > You can always use braces to separate a block without try at all. You can use braces in Java, but not in that language I'm talking about: it'll throw an error saying "Ambiguous expression could be a parameterless closure expression, an isolated open code block, or it may continue a previous statement; solution: Add an explicit parameter list, e.g. {it -> ...}, or force it to be treated as an open block by giving it a label, e.g. L:{...}, and also either remove the previous newline, or add an explicit semicolon ';'". The standalone-try looked far more elegant than: dudlabel:{ //do something } Better for that despot to re-enable standalone try-statements in Groovy than petitioning the Java 8 designers that thin arrows look better than industry-standard thick arrows.
- mattgreenrocks 15y ago> An empty try block allows us to repeat variable declarations with the same name in a long stretch of scripty-style code. Either I'm missing something here, or this is really as bad as it sounds. I understand you want nested scopes that end before the method's scope ends, but cannot for the life of me think of a defensible code example. Why not just make them into methods?
- ajuc 15y agoIn C++ block is often a way to ensure automatic variable destruction. This is important, because in C++ often there is important stuff going on in destructor. For example in RAII idiom. Example: ... ... { //critical section QMutexLocker locksInConstructorAndUnlocksInDestructor(&mutex); a = doStuff(a,b,c); b = doOtherStuff(a,b,c); c = andAnother(a,b,c); } .. .. It would be overkill to make this block a function, especially when code in critical section changes many variables.