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.
Joined: Jul 11, 2002
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.
Joined: Jul 01, 2003
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.