Exception slipping through the catch block?

Jonathan M Davis newsgroup.d at jmdavisprog.com
Fri Nov 9 00:27:40 UTC 2018


On Thursday, November 8, 2018 2:34:38 PM MST H. S. Teoh via Digitalmars-d-
learn wrote:
> On Thu, Nov 08, 2018 at 01:28:47PM -0700, Jonathan M Davis via 
Digitalmars-d-learn wrote:
> > On Thursday, November 8, 2018 10:55:45 AM MST Stanislav Blinov via
> >
> > Digitalmars-d-learn wrote:
> > > On Thursday, 8 November 2018 at 16:13:55 UTC, Mike Parker wrote:
> [...]
>
> > > > No, you should never catch Errors. They're separate for a
> > > > reason.
> > >
> > > Never say never :) There are legitimate cases for catching an
> > > Error or even a Throwable (for example, error propagation in a
> > > multi-threaded environment). However, this is not one of such
> > > cases.
> >
> > Yeah, but basically, the rule of thumb is never. Errors are fatal
> > error conditions which are supposed to terminate the program, and
> > programs should not be trying to recover from them. No one should be
> > attempting to catch them unless they know what they're doing, which
> > honestly, probably isn't going to be very many people for something
> > like this.
>
> Recently I ran into a case where catching Throwable makes sense: I have
> an Android application where the main code interfacing with the Android
> OS is written in Java, but program logic is written in D, called via
> JNI.  Since the JVM obviously cannot handle D exceptions, any unhandled
> D exception that makes it past the JNI boundary would cause the
> application to crash.  So what I did was to have the JNI interfacing
> code (on the D side) catch *everything*, i.e., Throwable, marshall the
> error message into a Java object, then send it via JNI back to the Java
> code that then displays the error message before aborting the
> application.  Extremely valuable in debugging, since otherwise I'd have
> to extract the stacktrace from the system logs, which is a pain.

You ran into one of the rare cases where it makes sense catch an Error or a
Throwable, and you're one of the few people who understands the situation
well enough to deal with it properly. The vast majority of D programmers
don't. Certainly, anyone who has to ask about the differences between
Throwable, Error, and Exception doesn't.

- Jonathan M Davis





More information about the Digitalmars-d-learn mailing list