Timeline for Is the 'finally' portion of a 'try ... catch ... finally' construct/structure even necessary?
Current License: CC BY-SA 3.0
16 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Feb 27, 2015 at 16:59 | history | edited | Doval | CC BY-SA 3.0 |
Inner catch block needs to throw `e2`, not `e`.
|
Jan 27, 2015 at 12:53 | history | edited | Doval | CC BY-SA 3.0 |
Changed Exception to Throwable
|
Jan 27, 2015 at 12:47 | comment | added | Doval |
@AgiHammerthief The nested try is inside the catch for the specific exception. Secondly, it's possible you don't know whether you can handle the error successfully until you've examined the exception, or that the cause of the exception also prevents you from handling the error (at least at that level). That's fairly common when doing I/O. The rethrow is there because the only way to guarantee cleanUp runs is to catch everything, but the original code would allow exceptions originating in the catch (SpecificException e) block to propagate upwards.
|
|
Jan 27, 2015 at 12:43 | comment | added | Martin James | @AgiHammerthief so that it gets logged by the top-level exception handler? | |
Jan 27, 2015 at 11:30 | comment | added | Agi Hammerthief |
Why are you using a nested try ... catch in your catch for a general exception? Surely a handleError() function that might throw an exception is badly coded? Why rethrow a caught exception? What am I missing?
|
|
Jan 27, 2015 at 11:28 | comment | added | DeadMG |
This answer doesn't really address the question of why C++ doesn't have finally , which is a lot more nuanced.
|
|
S Jan 27, 2015 at 7:36 | history | suggested | Nathan Tuggy | CC BY-SA 3.0 |
Added syntax highlighting
|
Jan 27, 2015 at 7:00 | review | Suggested edits | |||
S Jan 27, 2015 at 7:36 | |||||
Jan 27, 2015 at 1:24 | history | edited | Doval | CC BY-SA 3.0 |
added 30 characters in body
|
Jan 27, 2015 at 1:06 | comment | added | Alex |
I'd actually add the extra try-catch you omitted when calling handleErro(); which will make it even better argument as to why finally blocks are useful (even though that was not the original question).
|
|
Jan 26, 2015 at 18:41 | comment | added | njzk2 |
You may also be throwing an error. I would rephrase that catch (Throwable t) {} , with the try .. catch block around the entire initial block (to catch throwables from handleError as well)
|
|
Jan 26, 2015 at 18:13 | comment | added | supercat |
@JuriRobl: Indeed so. Incidentally, it may be interesting to note that nowadays Java compilers are required to rewrite try/catch/finally constructs into the form indicated above (including a catch within handleError ); this could in some situations involving N nested try/finally blocks require the cleanup code to be expanded out an exponential number of times.
|
|
S Jan 26, 2015 at 15:49 | history | edited | Doval | CC BY-SA 3.0 |
Footnotes.
|
Jan 26, 2015 at 15:34 | comment | added | Juri Robl |
You also have to catch the Exceptions in handleError() in the second case, no?
|
|
Jan 26, 2015 at 15:04 | review | Suggested edits | |||
S Jan 26, 2015 at 15:49 | |||||
Jan 26, 2015 at 13:17 | history | answered | Doval | CC BY-SA 3.0 |