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

Exception handling problem

saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689


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?


A = HARDWORK B = LUCK/FATE If C=(A+B) then C=SUCCESSFUL IN LIFE else C=FAILURE IN LIFE
SCJP 1.4
Jenna Thomas
Greenhorn

Joined: Oct 03, 2005
Posts: 27
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.


Cheers!
Jenn SCJP1.5
saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689
sorry i havn't got u
can explain it again
sachinb mahajan
Greenhorn

Joined: Jun 30, 2006
Posts: 6
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception handling problem