Scope failure is not preventing application crush in case of uncaught exceptions
Jonathan M Davis
newsgroup.d at jmdavisprog.com
Sat Dec 16 21:56:49 UTC 2017
On Saturday, December 16, 2017 21:22:47 kerdemdemir via Digitalmars-d-learn
wrote:
> On Saturday, 16 December 2017 at 20:56:26 UTC, ketmar wrote:
> > kerdemdemir wrote:
> >> As far as I know scope(failure) should be collecting all
> >> failure cases.
> >
> > nope. `failure` scope won't stop exception propagation, it is
> > just called before exception leaves your function, to give you
> > a last chance to do some immediate cleanup.
>
> Than do I still need to catch'em all? For me it doesn't matter
> the which exception it is. It is more important for me to not
> pollute my code with so many exception related lines. What is the
> most compact way for me to catch'em all?
The only way to catch an exception is with a catch block, and if you do
catch(Exception e)
then it will catch all exceptions derived from Exception (which is
everything that isn't an Error, though catching Errors is almost always a
bad idea).
You can also use something like std.exception.catchException like Ketmar
suggests, though that's just using a catch internally and returning the
Exception. There's nothing magical about it.
In general though, just eating exceptions is likely to cause problems. If an
exception is thrown, it's because there's a problem, and your program should
either be responding to it or exiting. Usually, you put try-catch statements
at the points in your program that are best suited for handling whatever
exceptions are likely to be thrown. If you feel the need to put tons of
try-catch statements in your program, you're probably doing something wrong.
Sometimes, lots of try-catch statements is appropriate, but usually, you
shouldn't need very many.
See Also:
http://ddili.org/ders/d.en/exceptions.html
http://ddili.org/ders/d.en/scope.html
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list