• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Constructors  RSS feed

 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!