在向上投射时捕获了错误的异常

时间:2015-07-23 11:04:16

标签: java exception

我的问题是上传时错误的捕获异常。我不知道什么是不正确的......

我创建了三个Exception,如下所示:

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();
    }
}

...我希望创建C对象并将此对象强制转换为A并捕获A异常。我的主要看起来像这样:

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);
        }
    }

在输出netbeans返回信息:

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)

我不知道它为什么会返回错误的异常......我尝试评论

            //} 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);
            }

...但它也会返回C异常。

2 个答案:

答案 0 :(得分:1)

在课程var secondstack = []; var positionTop = 20; var positionLeft = 20; document.body.style.background = '#00CCFF'; document.body.style.backgroundSize = "cover"; for(var i = 0; i < 25; i++){ var span = document.createElement("span"); span.style.position = "absolute"; span.style.left = positionLeft + "px"; span.style.top = positionTop + "px"; var div = document.createElement("div"); div.style.width = '105px'; div.style.height = '100px'; div.id = i; div.style.background = '#00FFCC'; $(div).css({backgroundSize: "cover"}); if((positionLeft + 250) < $(window).width()) positionLeft += 120; else { old_Width = positionLeft; var positionTop = 160 + positionTop; var positionLeft = 20; } span.appendChild(div); document.body.appendChild(span); } DivClick(); var resizeTimer; $(window).resize(function () { clearTimeout(resizeTimer); resizeTimer = setTimeout(doSomething, 100); DivClick() }); function rearrange(){ $("div").empty(); $("span").empty(); secondstack = []; var positionTop = 20; var positionLeft = 20; for(var i = 0; i < 25; i++){ var span = document.createElement("span"); span.style.position = "absolute"; span.style.left = positionLeft + "px"; span.style.top = positionTop + "px"; var div = document.createElement("div"); div.style.width = '105px'; div.style.height = '100px'; div.id = i; div.style.background = '#00FFCC'; $(div).css({backgroundSize: "cover"}); if((positionLeft + 250) < $(window).width()) positionLeft += 120; else { var positionTop = 160 + positionTop; var positionLeft = 20; } span.appendChild(div); document.body.appendChild(span); } } function DivClick() { $('div').on({click: function(){ var el = document.getElementById(this.id); el.style.border = "3px solid white"; if(!(secondstack.indexOf(this.id) > -1)) secondstack.push(this.id); else { var index = secondstack.indexOf(this.id); el.style.border = "3px solid black"; secondstack.splice(index, 1); } }}); } B中,您实际上会覆盖C课程中f()方法的定义。

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 的实例,并将其存储到类型为C的局部变量中。一旦调用A方法,它将在真实实例上执行,该实例为f(),而不是C(Java中覆盖规则)。

答案 1 :(得分:0)

这是正确的行为,是否会调用正确的实现:这是$('a.hashLink').on('click', function(e){ e.preventDefault(); var getUrlAfterHash = $(this).attr('href').split('#')[1]; var newURL = getUrlAfterHash +'.html'; window.history.pushState("", "", newURL); var idPosition = $('#' + getUrlAfterHash).offset(); $('html, body').animate({scrollTop: idPosition.top}, 800); }); 的概念