Java does not provide exception chaining on the language level.
try finally discards the exception of the try block should
there be an exception in the finally block.
This convention was taken from Common Lisp. But Common Lisp's
mechanisms are much more evolved. See the discussion 2009-01
comp.lang.lisp "Rationale behind unwind-protect and double errors"
If you want to simulate the convention of exception chaining in Java
you can use the second argument of error/2 for that purpose. Which
also solves
> catch(catch(..,
> my_exception(E), ...),
> cause(my_exception(E),_), ...))
>I did not find a way around for this problem. ...
There are actually two programming styles. One
style is using finally, and the other is
using catch/throw.
If you use catch/throw you have the option to do
exception chaining since Java 1.4 since the
exception base class Throwable offers the
cause field and corresponding enhanced constructors.
So basically before Java 1.4 a simple
exception could be only created as follows:
Throwable()
Throwable(String)
Subclasses can replace String by a subclass
specific Info. After Java 1.4 there is also:
Throwable(Throwable)
Throwable(String, Throwable)
The printStackTrace() method after Java 1.4
supports printing these chained exceptions
in a clever way by finding common frames
in the main exception and in the causing
exception.
** Question Begin **
Any Prolog arround that can do the same?
In my current implementation of cause/2
I just print twice the stack trace of
the first and second exception. Ok, it
might be not so difficult to do...
** Question End **
Since Throwable is part of java.lang it can
be said that since Java 1.4 exception chaining
is supported on the language level.
This exception chaining is heavily used nowadays
in Java and is very useful. Some prominent
chained exceptions are:
InvocationTargetException
ServletException (cause is called root cause there)
RemoteException (cause is called detail)
SQLWarning (cause not allowed for SQLException)
The programming pattern to create a chain
exception is very simple. It is not lightweight
if really an exception happens, since a new
object needs to be created:
try {
/* do something */
} catch (myExceptionInternal x) {
throw new myExceptionExternal("my exception external", x);
}
Although the programming pattern is creates a
new object in case of a chaining, the pattern
is very lightweight when no exception occurs,
since in Java try/catch does not incure any
overhead to running code when no exception occurs.
Only when an exception occurs the stack trace
and the try/catch region maps of the methods are
inspected.
*** Question Begin **
Any Prolog around that also has try/catch
region maps and thus lightweight exceptions? The
ISO standard suggests creating a choice point.
Would it be possible at all for Prolog to use
region maps in the presence of cut and/or
tail recursion?
*** Question End **
Since release Java 1.7 there is even something new ,
expressions in the chain can be marked as
suppressed. I guess it could be used to make a
non spurious finally by a try/catch. So that you
have some feature of the finally, that exceptions get
suppressed, and some feature of chained exceptions,
that they will be nevertheless shown in the top-level.
From the doc:
* In these situations, only one of the thrown exceptions can be
* propagated. In the {@code try}-with-resources statement, when
* there are two such exceptions, the exception originating from
* the {@code try} block is propagated and the exception from the
* {@code finally} block is added to the list of exceptions
* suppressed by the exception from the {@code try} block. As an
* exception unwinds the stack, it can accumulate multiple
* suppressed exceptions.
It looks that these suppressed exceptions not
exactly sit in the chain, but form an extra
info an exception can have. In Prolog we could
use suppressed/2 for that. Maybe should change
cause/2 of the cutter to suppressed/2 in the
first time... Good Idea! gr8
> If you want to simulate the convention of exception
> chaining in Java you can use the second argument
> of error/2 for that purpose. Which also solves
Only for error/2 balls. But not for arbitrary
balls. But maybe this might be ok.
Bye