Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Possible to create exceptions with full stack trace

 
Mark O' Sullivan
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Clapham
Sheriff
Pie
Posts: 20771
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheers, thanks very much.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic