Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Constructor

 
vikram Kesav
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
geeta vemula
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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....
 
Rajshekhar Paul
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
vikram Kesav
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all of you.

I got answer now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic