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

hig*_*der 15 java

我想问一下如何在我的界面中提及这一点

public class find(int x) throws A_Exception, B_Exception{
----
----
---
}
Run Code Online (Sandbox Code Playgroud)

我想说我可以在界面中提到一个异常,但是如何在我的界面中提到我的方法会抛出两个例外,即A和B ......

上面提到的代码片段仅适用于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 {
 ---------
}
Run Code Online (Sandbox Code Playgroud)

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

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

tan*_*ens 27

您可以根据需要为接口方法声明任意数量的异常.但是您在问题中提供的课程无效.它应该读

public class MyClass implements MyInterface {
  public void find(int x) throws A_Exception, B_Exception{
    ----
    ----
    ---
  }
}
Run Code Online (Sandbox Code Playgroud)

然后界面看起来像这样

public interface MyInterface {
  void find(int x) throws A_Exception, B_Exception;
}
Run Code Online (Sandbox Code Playgroud)