• Post Reply Bookmark Topic Watch Topic
  • New Topic

can constructor throw exceptions?  RSS feed

 
Ramakrishna Konanki
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.........


 
Rob Spoor
Sheriff
Posts: 21135
87
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.
 
Shanky Sohar
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..
 
Seetharaman Venkatasamy
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: 21135
87
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!