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

简介: 实现的方法中,抛的异常只能比父类或接口中的少 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/

目录
相关文章
|
云安全 安全 网络安全
|
15天前
|
JSON 程序员 数据格式
最新版Notepad++下载、安装、配置保姆级教程(附官网安装包)
Notepad++ 是一款免费开源、轻量快速的 Windows 文本编辑器,支持多标签、语法高亮、代码折叠与宏录制,体积小、启动快、资源占用低,兼容 x64/x32/ARM 架构,是程序员和文字工作者的高效利器。
3702 0
|
8天前
|
JavaScript 安全 前端开发
Nodejs Brotli decoder 代码执行漏洞 (CVE-2026-111497)10629
Node.js发布安全更新修复Brotli解码器高危RCE漏洞(CVE-2026-111497),建议受影响版本用户立即升级。
2049 2
|
15天前
|
人工智能 API 开发工具
DeepSeek Harness 神插件:白嫖 13 家大厂模型
`dsh-our-free-model` 是 DeepSeek Harness 神级插件:零配置、零API Key、零账号,聚合13家大厂免费模型(含DeepSeek V4.1 Flash、Kimi K3等),支持本地网关、用量看板与自动降级,真正实现“白嫖即用”。
2508 46
|
12天前
|
Web App开发 人工智能 前端开发
2026 年 AI 浏览器怎么选?Ego Lite、Tabbit、Chrome DevTools MCP 深度对比,开发者到底该用哪个?
2026年AI浏览器选型指南:Ego Lite专注多Agent协同(Mac优先),Tabbit国产首选,原生支持Windows与Codex控制;Chrome DevTools MCP非浏览器而是开发者利器,深度集成Console/Network/Performance调试能力,让AI真正“看懂并验证”前端代码。
1479 3
|
9天前
|
开发者 索引
DeepSeekHarness大肥鱼自带插件市场了?加个地址就能用
Open Plugin Market 是一个开放插件索引平台,收录800+优质DSH插件,支持分类浏览、智能推荐与随机发现。无需安装市场,粘贴索引地址即可一键接入,网页版直访 dshopen.org,助力小开发者被看见。(239字)
1987 1
DeepSeekHarness大肥鱼自带插件市场了?加个地址就能用
|
7天前
|
人工智能 Linux API
Magpie, 轻量本地AI网关!免费Token用不完!
Magpie 是开源AI编程模型调度网关,上线一月获1.8万Star。它通过本地高性能网关统一托管多平台API密钥、账号与免费额度,实现跨Agent的模型秒级切换、静默额度轮换与资源复用,显著降低碎片化管理成本。(239字)
Magpie, 轻量本地AI网关!免费Token用不完!
|
8天前
|
安全 应用服务中间件 Go
PHP2Shell 漏洞拆解:WordPress 与 nginx 为何中招
PHP2Shell 双 RCE 已遭在野利用:WordPress、Drupal 全在射程内,nginx 其实另有洞。拆成因与修复顺序
PHP2Shell 漏洞拆解:WordPress 与 nginx 为何中招
|
4天前
|
人工智能 安全 API
2026 年阿里云双 11|百炼大模型 API 指南:免费 Token 领取、API Key 生成、代码调用实战教程
对于零基础的AI开发者、技术爱好者和小型开发团队来说,低成本、低门槛调用主流大模型接口,是开启AI项目原型开发的必经之路。百炼大模型服务平台汇集了百余款主流大模型,面向新用户提供大规模免费推理额度,不需要预先充值,即可完成文本生成、逻辑推理、代码编写、多模态解析等各类开发验证工作。很多新手在初次接触平台时,常常遇到不清楚免费额度规则、不知道如何生成API‑Key、环境配置错误、调用接口持续报错等一系列问题。
620 1

热门文章

最新文章