db-derby-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Van Couvering <da...@vancouvering.com>
Subject Re: [jira] Commented: (DERBY-2181) StandardException.printStackTrace() does not print the nested exception (cause)
Date Sat, 16 Dec 2006 01:21:03 GMT
Hm, I would think printStackTrace() of the original exception should 
print the entire cause chain - as a matter of fact, this has been my 
observation, and I think Knut you even logged a bug when I got this wrong.

But for the chained SQLExceptions, yes, you do have to loop through them.

It sure seems like this should be rationalized so printStackTrace() 
gives you everything you need...

David

Knut Anders Hatlen (JIRA) wrote:
>     [ http://issues.apache.org/jira/browse/DERBY-2181?page=comments#action_12458963 ]

>             
> Knut Anders Hatlen commented on DERBY-2181:
> -------------------------------------------
> 
> I don't believe this is true for SQLException.getNextException(). SQLException maintains
separate chains for getNextException() and getCause(). Derby's JDBC 4.0 driver actually uses
both of them for different purposes. So in order to print all stack traces, something like
this is needed:
> 
> void printAllTraces(SQLException e) {
>   while (e != null) {
>     e.printStackTrace();
>     Throwable t = e.getCause();
>     while (t != null) {
>       if (t instanceof SQLException) {
>         printAllTraces((SQLException) t);
>       } else {
>         t.printStackTrace();
>       }
>       t = t.getCause();
>     }
>     e = e.getNextException();
>   }
> }
> 
>> StandardException.printStackTrace() does not print the nested exception (cause)
>> -------------------------------------------------------------------------------
>>
>>                 Key: DERBY-2181
>>                 URL: http://issues.apache.org/jira/browse/DERBY-2181
>>             Project: Derby
>>          Issue Type: Bug
>>    Affects Versions: 10.2.1.6
>>         Environment: Java 1.5
>>            Reporter: Christian d'Heureuse
>>
>> In my Servlet application, an I/O error occurred, but I couldn't see the error message
of the I/O error.
>> At line 298 in StandardException.java, the I/O exception is packed into a StandardException:
>>    throw StandardException.newException(
>>       SQLState.DATA_UNEXPECTED_EXCEPTION, ioe);
>> When my Servlet catches the exception, it calls Throwable.printStackTrace() to generate
a detailed error message. The output of printStackTrace() does not list the nested I/O exception.
>> In Java 1.5, Throwable.printStackTrace() calls getCause() to get the nested exception.
GetCause() returns null, because the cause has not been set.
>> One solution could be to override Throwable.getCause() and initCause() in StandardException
to get and set the nested exception. There are already getNestedException() and setNestedException()
methods in StandardException which could be mapped to getCause() and initCause().
>> Another solution could be to change the constructor
>>    StandardException(String messageID, Throwable t, Object[] args)
>> This constructor currently calls
>>    super(messageID);
>> Instead it could call
>>    super(messageID,t);
>> to pass the nested Exception.
>> StandardException.setNestedException() could be changed to call Throwable.initCause().
But the API docs of initCause() state stat "This method can be called at most once". So it's
probably better to override getCause() and initCause().
>> A problem might me that Throwable.getCause() and initCause() have been introduced
in Java 1.4. But if these methods are implemented in StandardException, they would be just
normal methods in Java 1.3 and overrides in Java >= 1.4.
> 

Mime
View raw message