实现的方法中,抛的异常只能比父类或接口中的少(转)

简介: 实现的方法中,抛的异常只能比父类或接口中的少 import java.io.IOException; public interface IHello { void say(String msg) throws TestException, IOException; } ...

实现的方法中,抛的异常只能比父类或接口中的少

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/

相关文章
|
2天前
|
人工智能 运维 安全
|
5天前
|
SpringCloudAlibaba 负载均衡 Dubbo
微服务架构下Feign和Dubbo的性能大比拼,到底鹿死谁手?
本文对比分析了SpringCloudAlibaba框架下Feign与Dubbo的服务调用性能及差异。Feign基于HTTP协议,使用简单,适合轻量级微服务架构;Dubbo采用RPC通信,性能更优,支持丰富的服务治理功能。通过实际测试,Dubbo在调用性能、负载均衡和服务发现方面表现更出色。两者各有适用场景,可根据项目需求灵活选择。
386 124
微服务架构下Feign和Dubbo的性能大比拼,到底鹿死谁手?
|
7天前
|
人工智能 JavaScript 测试技术
Qwen3-Coder入门教程|10分钟搞定安装配置
Qwen3-Coder 挑战赛简介:无论你是编程小白还是办公达人,都能通过本教程快速上手 Qwen-Code CLI,利用 AI 轻松实现代码编写、文档处理等任务。内容涵盖 API 配置、CLI 安装及多种实用案例,助你提升效率,体验智能编码的乐趣。
712 108
|
2天前
|
算法 Python
【轴承故障诊断】一种用于轴承故障诊断的稀疏贝叶斯学习(SBL),两种群稀疏学习算法来提取故障脉冲,第一种仅利用故障脉冲的群稀疏性,第二种则利用故障脉冲的额外周期性行为(Matlab代码实现)
【轴承故障诊断】一种用于轴承故障诊断的稀疏贝叶斯学习(SBL),两种群稀疏学习算法来提取故障脉冲,第一种仅利用故障脉冲的群稀疏性,第二种则利用故障脉冲的额外周期性行为(Matlab代码实现)
228 152
|
4天前
|
Java 数据库 数据安全/隐私保护
Spring 微服务和多租户:处理多个客户端
本文介绍了如何在 Spring Boot 微服务架构中实现多租户。多租户允许单个应用实例为多个客户提供独立服务,尤其适用于 SaaS 应用。文章探讨了多租户的类型、优势与挑战,并详细说明了如何通过 Spring Boot 的灵活配置实现租户隔离、动态租户管理及数据源路由,同时确保数据安全与系统可扩展性。结合微服务的优势,开发者可以构建高效、可维护的多租户系统。
203 127
|
4天前
|
Web App开发 前端开发 API
在折叠屏应用中,如何处理不同屏幕尺寸和设备类型的样式兼容性?
在折叠屏应用中,如何处理不同屏幕尺寸和设备类型的样式兼容性?
231 124
|
2天前
|
编解码 算法 自动驾驶
【雷达通信】用于集成传感和通信的OFDM雷达传感算法(Matlab代码实现)
【雷达通信】用于集成传感和通信的OFDM雷达传感算法(Matlab代码实现)
174 125
|
2天前
|
JavaScript 关系型数据库 MySQL
基于python的网上外卖订餐系统
本系统基于Python与Flask框架,结合MySQL数据库及Vue前端技术,实现了一个功能完善的网上订餐平台。系统涵盖餐品、订单、用户及评价管理模块,并深入研究订餐系统的商业模式、用户行为与服务质量。技术上采用HTML、PyCharm开发工具,支持移动端访问,助力餐饮业数字化转型。