• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Constructors

 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,
Hopefully a basic question this.. are constructors allowed to catch/throw exceptions?
Regards,
prasad.
 
Ranch Hand
Posts: 275
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can have a constructor which throws exception using 'throws' keyword, which shows the constructor is capable of raising the exception but the exception must be handled by the calling method.
It means in your main method create object of the class inside try and catch the exception in catch block correspondint to it.
Hope you got my point.
 
Nagendra Prasad
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the response... Got your point... no problems with that.. and the logic u have described is how I have actually implemented it.. but I what I was wondering was is it a good practice ( best practice... philosophically correct.. whatever..) to have constructors throwing exceptions?
Regards,
Prasad.
 
Grishma Dube
Ranch Hand
Posts: 275
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It depends. But in most of the cases, it makes a perfect sense. Especially when the constrctor is taking intial size as a Parameter, it that size is negative then immediately it will throw an exception.
In a way, its a good practice.
 
reply
    Bookmark Topic Watch Topic
  • New Topic