• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Possible to create exceptions with full stack trace

 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 28331
97
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cheers, thanks very much.
 
When it is used for evil, then watch out! When it is used for good, then things are much nicer. Like this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic