简体   繁体   中英

Wrong Exception catched when up-casting

My problem is about wrong catched exception when up-casting. I have no idea what is incorrect...

I create three Exception s which looks like this :

class A extends Exception{
    public void f() throws A{
        System.out.println("Exception from A()");
        throw new A();
    }
    public void g() throws A{
        System.out.println("Exception from A()");
        throw new A();
    }
}
class B extends A{
    @Override
    public void f() throws B{
        System.out.println("Exception from B()");
        throw new B();
    }
}
class C extends B{
    @Override
    public void f() throws C{
        System.out.println("Exception from C()");
        throw new C();
    }
}

...and I want create C object and cast this object to A and catch A exception. My main looks like this :

public static void main(String[] args) {
        try {
            C obj = new C();
            ((A)obj).f(); // cast object C --> A....  Why it isn't work ?!
                            // should catch exception A not C !!!
                            // problem is when f() method is overrided by subclass

//            ((A)obj).g(); // working CORRECT when use other method...

//           A obj2 = (A) obj; // I try other casting type
//           obj2.g(); //method g() from exception A - work CORRECT, exception A catched...

        } catch (C e) { //third in hierarchy
            e.printStackTrace(System.err);
        } catch (B e) { //second..
            e.printStackTrace(System.err);
        } catch (A e) { //base
            e.printStackTrace(System.err);
        }
    }

At the output netbeans returns info :

Exception from C()
exceptions.C
    at exceptions.C.f(HierarchyExceptions.java:24)
    at exceptions.HierarchyExceptions.main(HierarchyExceptions.java:32)
BUILD SUCCESSFUL (total time: 0 seconds)

I have no idea why it returns wrong exception... I try comment

            //} catch (C e) { //third in hierarchy
            //    e.printStackTrace(System.err);
            //} catch (B e) { //second..
            //    e.printStackTrace(System.err);
            } catch (A e) { //base
                e.printStackTrace(System.err);
            }

...but it returns C exception too.

In classes B and C you are effectively overriding the definition of the f() method from class A .

C obj = new C();
((A)obj).f(); // cast object C --> A....  Why it isn't work ?!
              // should catch exception A not C !!!
              // problem is when f() method is overrided by subclass

This code will create an instance of class C and store it into local variable of type A . Once you are calling the f() method it will be executed on the real instance which is C , not A (the rules of overriding in Java).

它是正确的行为,是否强制转换是否调用了正确的实现:这是Dynamic method dispatch的概念

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM