在java中的一个接口的方法中抛出多个异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2168904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:22:11  来源:igfitidea点击:

Throwing multiple exceptions in a method of an interface in java

java

提问by higherDefender

I wanted to ask that how to mention this in my interface

我想问一下如何在我的界面中提到这个

public class find(int x) throws A_Exception, B_Exception{
----
----
---
}

i want to say that i can mention One exception in an interface but how to mention in my interface that my method will throw Two exceptions Namely A and B...

我想说我可以在接口中提到一个异常,但是如何在我的接口中提到我的方法将抛出两个异常,即 A 和 B ...

the code fragment mentioned above works only for A and not for B... Help

上面提到的代码片段仅适用于 A 而不适用于 B... 帮助

public interface dictionary{ 
   public void insert(int x) throws Dictionary_FullException, Duplicate_Element_FoundException; 
}
...
 public class SortedArray implements dictionary{ 
   public void insert(int x) throws Dictionary_FullException, Duplicate_Element_FoundException {
 ---------
}

but when i compile it ... it says..

但是当我编译它时......它说..

SortedArray.java:66: insert(int) in assign.SortedArray cannot implement insert(int) in assign.dictionary; overridden method does not throw assign.SortedArray.Duplicate_Element_FoundException public void insert(int x) throws Dictionary_FullException

SortedArray.java:66:assign.SortedArray 中的insert(int) 不能在assign.dictionary 中实现insert(int);重写的方法不会抛出assign.SortedArray.Duplicate_Element_FoundException public void insert(int x) throws Dictionary_FullException

采纳答案by tangens

You can declare as many Exceptions as you want for your interface method. But the class you gave in your question is invalid. It should read

您可以为接口方法声明任意数量的异常。但是您在问题中给出的课程无效。它应该读

public class MyClass implements MyInterface {
  public void find(int x) throws A_Exception, B_Exception{
    ----
    ----
    ---
  }
}

Then an interface would look like this

然后一个界面看起来像这样

public interface MyInterface {
  void find(int x) throws A_Exception, B_Exception;
}

回答by nos

You need to specify it on the methods that can throw the exceptions. You just seperate them with a ',' if it can throw more than 1 type of exception. e.g.

您需要在可以抛出异常的方法上指定它。如果它可以抛出超过 1 种类型的异常,您只需用 ',' 将它们分开。例如

public interface MyInterface {
  public MyObject find(int x) throws MyExceptionA,MyExceptionB;
}

回答by TofuBeer

I think you are asking for something like the code below:

我想你是在要求类似下面的代码:

public interface A
{
    void foo() 
        throws AException;
}

public class B
    implements A
{
    @Overrides 
    public void foo()
        throws AException,
               BException
    {
    }
}

This will not work unless BException is a subclass of AException. When you override a method you must conform to the signature that the parent provides, and exceptions are part of the signature.

除非 BException 是 AException 的子类,否则这将不起作用。当您重写一个方法时,您必须符合父级提供的签名,并且异常是签名的一部分。

The solution is to declare the the interface also throws a BException.

解决办法是声明接口也抛出BException。

The reason for this is you do not want code like:

这样做的原因是你不想要这样的代码:

public class Main
{
    public static void main(final String[] argv)
    {
        A a;

        a = new B();

        try
        {
            a.foo();
        }
        catch(final AException ex)
        {
        }
        // compiler will not let you write a catch BException if the A interface
        // doesn't say that it is thrown.
    }
}

What would happen if B::foo threw a BException? The program would have to exit as there could be no catch for it. To avoid situations like this child classes cannot alter the types of exceptions thrown (except that they can remove exceptions from the list).

如果 B::foo 抛出 BException 会发生什么?该程序将不得不退出,因为它无法捕获。为了避免这种情况,子类不能改变抛出的异常类型(除了它们可以从列表中删除异常)。