异常与 throws 子句不兼容的原因是什么?

编程入门 行业动态 更新时间:2024-10-26 18:21:19
本文介绍了异常与 throws 子句不兼容的原因是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

谁能告诉我异常可能有什么原因,与throws"子句不兼容

Can anyone tell me what reasons exceptions can have, not to be compatible with "throws" clauses

例如:

class Sub extends Super{ @Override void foo() throws Exception{ } } class Super{ void foo() throws IOException{ } }

Exception Exception 与 Super.foo() 中的 throws 子句不兼容

推荐答案

没有完整的代码示例,我只能猜测:你在子类中重写/实现了一个方法,但是子类方法的异常规范不兼容与(即不是超类/接口方法的子集)?

Without a full code sample, I can only guess: you are overriding/implementing a method in a subclass, but the exception specification of the subclass method is not compatible with (i.e. not a subset of) that of the superclass/interface method?

如果声明基方法根本不抛出异常,或者例如java.io.IOException(它是您的方法试图在这里抛出的 java.lang.Exception 的子类).基类/接口的客户端希望其实例遵守基方法声明的契约,因此从该方法的实现中抛出 Exception 会破坏契约(并且 LSP).

This can happen if the base method is declared to throw no exceptions at all, or e.g. java.io.IOException (which is a subclass of java.lang.Exception your method is trying to throw here). Clients of the base class/interface expect its instances to adhere to the contract declared by the base method, so throwing Exception from an implementation of that method would break the contract (and LSP).

更多推荐

异常与 throws 子句不兼容的原因是什么?

本文发布于:2023-11-26 03:21:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1632431.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:子句   不兼容   异常   原因   throws

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!