wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Cannot figure out the result Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Cannot figure out the result" Watch "Cannot figure out the result" New topic
Author

Cannot figure out the result

Akshay Singhvi
Ranch Hand

Joined: Nov 08, 2005
Posts: 93
Hello Ranchers,

Herein I'm writing 2 programs.
Prog1:
////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////

The result is compiler error stating
./scjp/Exception/Math.java:9: exception scjp.Exception.Math.MyException is never
thrown in body of corresponding try statement
catch(MyException e){P.rintln("1");}

/////////////////////////////////////////////////////////////////////

while Prog 2:



//////////////////////////////////////////////////////////////////

It compiles fine and result is 3

Plz explain why the disparity in answers???

Regards ,
Akshay

EDIT by mw: Added Code Tags.
[ December 10, 2005: Message edited by: marc weber ]

Regards,<br />Akshay Singhvi<br />SCJP 1.4 (95%)<br />SCWCD 1.4 (86%)
gutta rahul
Greenhorn

Joined: Nov 23, 2003
Posts: 12
In the first sample the reference "m" given is of type MathObj, which is resolved at compile time, which is static binding. In the second sample the reference "m" is of super type Math and is resolved at runtime, which is late binding.
tim bond
Greenhorn

Joined: Nov 21, 2005
Posts: 18
public class Math{ public class MyException extends Exception{}
public static void main(String[] args){
MathObj m=new MathObj();
try{ m.op();}
catch(MyException e){ System.out.println("1");} }
public void op()throws MyException{
System.out.println("2");throw new MyException();}}
class MathObj extends Math{
public void op(){ System.out.println("3");}}

in the first m is object of Mathobj and reference of MathObj. to resolve the error
you need to remove the System.out.println("2");throw new MyException();
to System.out.println("2"); as the exception is already in try and catch block.
there is no need to throw it again. This is causing the error.

public class Math{
public class MyException extends Exception{}
public static void main(String[] args){
Math m=new MathObj(); try{ m.op();}
catch(MyException e){ System.out.println("1");} }
public void op()throws MyException{
System.out.println("2");throw new MyException();}}
class MathObj extends Math{
public void op(){ System.out.println("3");}}


here the m is object of Math but reference of MathObj. so it just executes the m.op in class mathobj .
tim bond
Greenhorn

Joined: Nov 21, 2005
Posts: 18
or you have to modify the public void op()throws MyException{
System.out.println("2");throw new MyException();}}
to
public void op(){
System.out.println("2");throw new MyException();}}
this will then print 2 and 1 ;
Akshay Singhvi
Ranch Hand

Joined: Nov 08, 2005
Posts: 93
hello Ranchers,

I'm not statisfied with the response.Plz elaborate further.
As far as Tim response is concerned I guess ,he mess up the question.
My doubt is basically on this line

Math m=new MathObj();
&
MathObj m=new MathObj();

In first line the method op() is called of class MathObj because of polymorphism and late binding,but also in 2nd line op() is called of MathObj.Isn't it???
Then why the disparity???
What does compilation error has to do with static binding or late binding

Regards,
Akshay
Thomas Drew
Ranch Hand

Joined: Sep 15, 2004
Posts: 47
Akshay , you are overriding the op option in MathOpt. The Op in MathOpt does not throw an exception. Where as the option in Math does. To fix the problem you will have to either throw the exception in the MathOpt class, remove the statement from the try block, or call the op option in the super class. If you elect to have the class throw an exception you will have to change the class Mathopt to throw an exception as well.
Hope this helps.
Akshay Singhvi
Ranch Hand

Joined: Nov 08, 2005
Posts: 93
Hello Ranchers,

Regarding my problem and Thomas's response,my doubt isn't yet cleared.
Specifically , my doubt is why the disparity on the output by just changing the reference of m (first MathObj and second Math)since both the time MathObj's op is being called.
Regards ,
Akshay
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Cannot figure out the result
 
Similar Threads
Please tell me where is the error in this program
Exception error
Exception
Exception thrown from catch and finally block...
Reg. Exception hierarchy