Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Throwing multiple exceptions in a method of an interface in java

Tags:

java

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...

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

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

like image 821
higherDefender Avatar asked Jan 30 '10 18:01

higherDefender


People also ask

Can a method throw multiple exceptions Java?

A method can throw one of several exceptions. Eg: public void dosomething() throws IOException, AWTException { // .... } This signals that the method can eventually throw one of those two exceptions (and also any of the unchecked exceptions).

How do you declare multiple exceptions in Java?

You can use vertical bar (|) to separate multiple exceptions in catch block. An old, prior to Java 7 approach to handle multiple exceptions.

How many exceptions a method can throw?

As seen in the syntax above, all exceptions that can be thrown by a method should be declared in the method signature using the throws keyword. A method can throw multiple exceptions, which should be separated by a comma in the declaration.

Can you declare multiple exceptions in a method header?

Yes, declare multiple exceptions in a method header are possible. If the method declares multiple exceptions, add list of the exceptions, separated by commas, after throws.


3 Answers

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;
}
like image 198
tangens Avatar answered Sep 18 '22 04:09

tangens


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.

The solution is to declare the the interface also throws a 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).

like image 44
TofuBeer Avatar answered Sep 20 '22 04:09

TofuBeer


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.

public interface MyInterface {
  public MyObject find(int x) throws MyExceptionA,MyExceptionB;
}
like image 25
nos Avatar answered Sep 17 '22 04:09

nos