• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception handling problem

 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


When i use parent class reference hte method getInt needs to keep in try catch block and when i use child class reference i need not kepp in try catch block

can any one explain me why i need to do this?
 
Jenna Thomas
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When overriding a method, u can choose to have the overridden method in the child throw no exceptions (as in the case of Ping::getInt() which throws no exceptions). Hence we dont need to keep the try catch block for the child method or you could keep the try catch block anyways, it does no one any harm.

Also, imagine you need to do something like
Utils p= new Ping();

You would still need the try catch when u invoke p.getInt().

Hope that answers your question.
 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry i havn't got u
can explain it again
 
sachinb mahajan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At compilation time the reference type decides the exception needs to be catched

e.g.

Ping u = new Ping();

since Ping is a child class and the refrence of the child class is used to invoke method

u.getInt(args[0])

in the child class method getInt doesn't throw any exception so you dont need to catch it.

if you would have had reference of the Util class

e.g.

Util u = new Util();

then while calling method getInt you need to have try catch block as it throws the excpetion.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic