Meaningless Drivel is fun!*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Possible to create exceptions with full stack trace Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Possible to create exceptions with full stack trace" Watch "Possible to create exceptions with full stack trace" New topic
Author

Possible to create exceptions with full stack trace

Mark O' Sullivan
Ranch Hand

Joined: Aug 17, 2009
Posts: 160
Hi,
In my assignment, it states for exceptions,
Any unimplemented exceptions in this interface must all be created as member classes of the suncertify.db package. Each must have a zero argument constructor and a second constructor that takes a String that serves as the exception's description.

I read though if functions can be called from anywhere like they are in this assignment, a full stack trace is more useful. In this case, is it possible to create exceptions with the full stack trace?
For e.g. signiture: public RecordNotFoundException(String exceptionDescription,Throwable e)
Thanks.
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2258
    
    3

Champion, I definitely agree that in real life, it is pretty useful to have other constructors... in some cases. Here's what I think: if you have to protect your API and hide a particular implementation (like, you have a RepositoryException, and the data can come from an Oracle database or from an XML file), then it is a good idea to have at least a constructor like RepositoryException(Throwable cause). But sometimes, the exception is going to start from a particular condition. In our case, we have to analyze if a record matches a particular search criteria. If no records match, then the exception is going to start there, and we don't really have to wrap other exceptions... so, in this case, I guess it really isn't necessary. Some exceptions of the Java API only have these two constructors (see the ClassCastException, for instance). Another thing, I'm not really sure if we are allowed to provide other constructors. Even though the instructions do not say that we can't, I myself only provided these two constructors.


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Mark O' Sullivan wrote:I read though if functions can be called from anywhere like they are in this assignment, a full stack trace is more useful. In this case, is it possible to create exceptions with the full stack trace?
For e.g. signiture: public RecordNotFoundException(String exceptionDescription,Throwable e)
Thanks.


I think you're misunderstanding. When you create a new exception, it automatically has the full stack trace already. You don't have to do anything special to make that happen. Try this code and you'll see that:


However it is possible to wrap another exception if you already have one, i.e. if you caught one and want to throw a different one. In that case you'd want to have your constructor delegate to the same constructor in the Exception class. (You are extending Exception, aren't you?) When you do that, whatever catches that different one will have access both to its stack trace and the stack trace of the exception that you wrapped.
Mark O' Sullivan
Ranch Hand

Joined: Aug 17, 2009
Posts: 160
Cheers, thanks very much.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Possible to create exceptions with full stack trace
 
Similar Threads
Failed to resolve ejb-link
applet access file on his server?
applet error
Does Struts have a log? Can't figure out why ActionServlet is not initializing
Parsing encoded data?