File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Java Constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Java Constructor" Watch "Java Constructor" New topic
Author

Java Constructor

vikram Kesav
Ranch Hand

Joined: Jul 11, 2008
Posts: 69


Can any one tell me what is the problem in this code?

When i am compiling code it is giving error like - B must catch exception....

I am not sure what going on.....i need complete explanation.... I need your help Ranchers.
ramesh maredu
Ranch Hand

Joined: Mar 15, 2008
Posts: 210

check the constructor of B, it declared FileNotFoundException which is checked exception so you are getting compile time error.You need to either declare it or handle it.


SCJP 1.5 94%.
The greatest glory in living lies not in never falling, but in rising every time we fall.
geeta vemula
Ranch Hand

Joined: Jul 18, 2008
Posts: 208
here the code may throw the exception. So you must write the code to handle it (try and catch) or else you must declare main to throw the excetion.
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362


when you are creating object for class B
At first the constructor of class B is executed in which it executes super();
As a result it executes the super class constructor that mean class A's constructor in which it is throwing filenotfoundexception
so class B's constructor should handle or declare that exception..
here it is throwing that exception to the method from which you called class B's constructor..
but main method is not declaring as well as not handling that filenotfoundexception that why the compiler gives you error....


SCJP5 and SCWCD1.5
Think Twice Act Wise...
Rajshekhar Paul
Ranch Hand

Joined: Oct 17, 2006
Posts: 140
The fact is when you are invoking a method which throws checked exception, you need to either handle or declare that exception in the calling method.


When it's obvious that you have to do it, just do it without shattering your thoughts over different directions.
vikram Kesav
Ranch Hand

Joined: Jul 11, 2008
Posts: 69
Thanks for all of you.

I got answer now.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Constructor