Description
The SlingException constructors taking Throwable arguments both call Throwable.initCause(Throwable) after calling the actual super-constructor.
This dates back to Rev. 609649 (Jan. 7th, 2008!) where I added this to make sure the Throwable class is initialized with the given cause. At that time SlingException extended from ServletException which does not do proper Java 1.4 exception chaining but implements its own chaining.
Later with SLING-186 (Rev. 614480, Jan. 23rd, 2008) SlingException was modified to extend from RuntimeException which would do proper chaining. But the call to Throwable.initCause(Throwable) was not removed back then. So this method is still called when a SlingException is instantiated with a Throwable.
The problem comes from the implementation of the Throwable though, which throws IllegalStateException if initCause(Throwable) is called after the cause has already been set.
Throwable sets the cause field to this to indicate the cause has not been set yet. But getCause() returns null both for no cause having been set or the cause being null.
If the SlingException is instantiated with a null cause, this sets the Throwable.cause field to nul thus returning null on getCause() thus causing the SlingException to call initCause. This in turn causes initCause(Throwable) to throw IllegalStateException because the cause has already been set.
Long story short: SlingException must not call initCause because that is not needed anymore since SLING-186/Rev. 614480