DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/)
-   Java (http://www.daniweb.com/forums/forum9.html)
-   -   Unhandled Excepton (http://www.daniweb.com/forums/thread91541.html)

tapannigam Oct 3rd, 2007 9:19 am
Unhandled Excepton
 
class A{
void process() throws Exception{
throw new Exception();
}
}
public class ExtendTest extends A{

void process(){
System.out.print("ExtendTest");
}
public static void main(String[] args) {
A a = new ExtendTest();
a.process(); //line 1
new ExtendTest().process(); // line 2
}
}
Why does this program gives an unhandled Exception at line 1? Line 1 and Line 2 are using same object.

nschessnerd Oct 3rd, 2007 9:49 am
Re: Unhandled Excepton
 
Line 1 is an A object. a throws the error. When you declare an object like this i dont think the methods are overridden.
try ExtendTest a = new ExtendTest();

darkagn Oct 3rd, 2007 10:17 am
Re: Unhandled Excepton
 
You have assigned the variable a as an object of type A. A's process() method throws an exception. You need to wrap the line
a.process();
in a try/catch block. Let me know if this hasn't helped.
Also,
new ExtendTest().process();
doesn't make sense. I think what you want is
 ExtendTest et = new ExtendTest();
et.process();


All times are GMT -4. The time now is 1:20 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC