aspose file tools*
The moose likes Beginning Java and the fly likes Constructors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Constructors" Watch "Constructors" New topic
Author

Constructors

Nagendra Prasad
Ranch Hand

Joined: Jul 11, 2002
Posts: 219
Hello All,
Hopefully a basic question this.. are constructors allowed to catch/throw exceptions?
Regards,
prasad.


Best Regards,<br />Nagendra Prasad.
Grishma Dube
Ranch Hand

Joined: Jul 01, 2003
Posts: 275
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

Joined: Jul 11, 2002
Posts: 219
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

Joined: Jul 01, 2003
Posts: 275
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructors