aspose file tools*
The moose likes Java in General and the fly likes wierd code snippet about NestedException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "wierd code snippet about NestedException" Watch "wierd code snippet about NestedException" New topic
Author

wierd code snippet about NestedException

Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
Hi all,

From the site,
http://www-106.ibm.com/developerworks/java/library/j-ejb01283.html

I am reading the code for getMessage() in ApplicationException class. Is not that implemented weirdly? Why we need while loop? Observe closely while loop in each child ApplicationException is called only once given the hierarchy of objects as,

AE->AE->AE->SomeCheckedException (where AE is ApplicationException object)

Is the following code (I wrote) doing same thing without unnecessary code?



I hope I am able to explain my confusion...

Regards
Maulin
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
No, the while loop is necesssary here because you may have exceptions nested in exceptions nested in exceptions, etc. Each time through the while loop, it looks at the exception one lever lower than the current one. Note at the end of the while loop: parent = child. This ensures that the next time through the loop, it will get the child of the new parent (the former child). Until it gets a null, indicating there are no more nested exceptions to traverse.


"I'm not back." - Bill Harding, Twister
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
okay but the code I have given does the same thing. Doesn't it?

It keeps calling cause.getMessage() right now, for cause object there are two possibilities,

1. it is a NestedException
2. it is a standard java exception (not an NestedException)

in both cases getMessage() would work. In first it will call corresponding NestedException's getMessage() (recursion) and in second case it will call standard getMessage() of any exception right?

So essentially it is same effect.

Also, here is the iterative version I have come up with,


Thanks
maulin
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
okay but the code I have given does the same thing. Doesn't it?

No. For one thing it's an if statement, not a loop of any kind. So the if clause can execute once, or not at all; no more. Just as importantly though, I think you've confused different types of hierarchies here. The idea of a nestable exception is that it contains a nested exception. As opposed to extending it. I suppose it's author Brett McLaughlin's fault here, since for some reason he called a method he called getNestedException(), without ever bothering to define it. But the concept of "nesting" refers to embedding exceptions inside other exceptions. It's containment, not inheritance. In this context the implementation of getNestedException() should be:

It's worth noting that getCause() was built into JDK 1.4, and the entire article is really geared to earlier JDKs, since there no need to define this stuff if you're using JDK 1.4+. (Which is why I'm sure I know what "nested" refers to here - it's been built into the language for a while now.) Anyway, the point is that getNestedException has nothing, nothing to do with super.getMessage(). We're not trying to access an overridden method here; we're trying to access another exception contained in the ApplicationException. That was the point of adding a "cause" parameter to the ApplicationException, and providing constructors and methods to set it. That's the nested exception, which we're trying to get at.
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
Jim,

Can you trace my codes - both recursive and iterative w/ pen and paper and see if it is really not working.

I think both are correct. I don't think I am confused about either extends OR contains relation here.

I have trace all snippets w/ pen and paper before posting so I would wish if you can give it a shot if you have not already and if you did , please tell me exactly where it doesn't work.

Meanwhile I am pondering over the last post from you more..

Thanks
Maulin
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
oh by the way,

here is the method i have defined, (of course i notice that original code didnt have getNestedException() method defined, I can't afford to overlook while developing framework used by my peers )

public Throwable getNestedException(Throwable t) {

if ( t instanceof NestedException) {
return t;
} else {
return null;
}

I can post my whole code tomorrow as its on my office computer ...

Thanks
maulin
}
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
ooopsss sorry... i meant to write,

return t.cause; in getNestedException() not just return t;
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
I guess I might be giving wrong impression. I am actually using code similar to http://www.javaworld.com/javaworld/javatips/jw-javatip91.html

article and NOT from IBM's article but when I read it I thought why we have to run while loop there ...

I think it might be better if I post my code. I will do that tomorrow morning first thing.

Sorry for any confusion this might have created but I guess now I am more clear..

Thanks
maulin
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
Hi all,

Here is the nested exception I have written,



does it sound correct?

Thanks
Maulin
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: wierd code snippet about NestedException
 
Similar Threads
uncompleted article !
Excellent JSTL article from ibm developerworks
How to use Concurrency Utilities ??
Classpath
lifecycle of session and request