實現的方法中,拋的異常只能比父類或介面中的少(轉)

developerguy發表於2016-01-13

實現的方法中,拋的異常只能比父類或介面中的少

import java.io.IOException;

public interface IHello {
    void say(String msg) throws TestException, IOException;
}

class IHelloImpl implements IHello {

    @Override
    public void say(String msg) throws IOException {
        System.out.println("msg = [" + msg + "]");
    }
}

 

 

類StormyInning既擴充套件了基類Inning,又實現了介面Storm,而且基類Inning和介面Storm都宣告瞭方法event():

package com.liush.chapter12;

 

//Overridden methods may throw only the exceptions specified in their

//base-class versions,or exceptions derived from the base-class exceptions.

 

class BaseballException extends Exception {}

class Foul extends BaseballException {}

class Strike extends BaseballException {}

abstract class Inning {

    public Inning() throws BaseballException {}

    public void event() throws BaseballException {

        // Doesen`t actually have to throw anthing

    }

    public abstract void atBat() throws Strike,Foul;

    public void walk() {} //Throws no checked exceptions

}

 

class StormException extends Exception {}

class RainedOut extends StormException {}

class EventException extends StormException {}

class PopFoul extends Foul {}

 

interface Storm{

    public void event() throws EventException;

    public void rainHard() throws RainedOut;

}

 

public class StormyInning extends Inning implements Storm{

    //OK to add new exceptions for constructor, but you must

    //deal with the base constructor exceptions:

    public StormyInning() throws RainedOut,BaseballException {}

    public StormyInning(String s) throws Foul,BaseballException {}

    

    @Override

    public void rainHard() throws RainedOut {

        // TODO Auto-generated method stub

        

    }

    public void event() {System.out.println(“Override the Inning.”);}

    @Override

    public void atBat() throws Strike, Foul {

        // TODO Auto-generated method stub

        

    }

    public static void main(String[] args) {

        try{

            StormyInning si = new StormyInning();

            si.atBat();

        }

        catch(PopFoul e) {System.out.println(“Pop foul”);}

        catch(RainedOut e) {System.out.println(“Rained out”);}

        catch(BaseballException e) {System.out.println(“Generic baseball exception”);}

        

        try{

            Inning i = new StormyInning();

            Storm k=new StormyInning();

            i.event();

            k.event();

            i.atBat();

        }

        catch(Strike e) { System.out.println(“Strike”);}

        catch(Foul e) { System.out.println(“Foul”);}

        catch(RainedOut e) { System.out.println(“Rained out”);}

        catch(BaseballException e) { System.out.println(“Generic baseball exception”);}

        catch (EventException e) {    // TODO Auto-generated catch block

            e.printStackTrace();

        }

    }

}

說明:類StormyInning既繼承了Inning基類,又實現了Storm介面,用基類來建立StormyInning物件時,根據向上轉型,event()是被認為是從Inning中來:

而用介面Storm來建立StormyInning物件時,event()則是被認為是從Sorm中來的:

因此,它們丟擲異常就決定於它們的基類和介面了:

 

http://blog.163.com/liu_sheng_han/blog/static/190591372201212394856740/


相關文章