Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

can constructor throw exceptions?  RSS feed

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


the above program works fine......but my question is
1)as i studied in a book,a constructor can not handle exception----------above code throwing exception
2)a constructor can not call method(even in the same class),
except super class constructor, but it should be the first line(super()).............How it works with new IllegalArgmentException
3) a constructor cannot return values......

in the above code........how constructor handled exception.....
i required the flow of execution.........


 
Sheriff
Posts: 21461
96
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags next time. I've added them for you this time.

Ramakrishna Konanki wrote:the above program works fine......but my question is
1)as i studied in a book,a constructor can not handle exception----------above code throwing exception
2)a constructor can not call method(even in the same class),
except super class constructor, but it should be the first line(super()).............How it works with new IllegalArgmentException
3) a constructor cannot return values......

in the above code........how constructor handled exception.....
i required the flow of execution.........


Can you please QuoteYourSources? Because either the book is wrong, or you misinterpreted it.

A constructor can throw exceptions without a problem. Your code proves that. A constructor can call methods in its constructor*. The only thing a constructor cannot do is catch exceptions thrown by the super() constructor. That's because the call to super() / this() must be the first statement in the constructor, and a try-catch-finally block is a statement on its own. As such, the super() / this() would become the first statement inside the first statement, and that's not the same as the first statement. After the call to super() or this(), just about any code is possible.
You are correct about constructors not being able to return values. They can include return statements, but not with a value. It's like adding return statements to a void method.


* although this should be limited to private or final methods; calling overridable methods in the constructor is dangerous because the fields of the subclass will not be initialized yet.
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
one more thing about constructor..you cannot directly call construtor..
 
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wonderful explanation Rob!
 
Rob Spoor
Sheriff
Posts: 21461
96
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Shanky Sohar wrote:one more thing about constructor..you cannot directly call construtor..


Actually you can, with the keyword "this":
However, just like super(...) it must be the first statement in the constructor if present. That means that super() and this() together are not allowed.

Seetharaman Venkatasamy wrote:Wonderful explanation Rob!


Thank you!
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Prime wrote:
Actually you can, with the keyword "this":



This is correct but not like what i show above.i.e by their name.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!