Java: How can I declare an interface method that can throw an exception?

Let's say I have this interface A, which is implemented by several providers:

interface A
{
    void x();
    void y();
}

However, I want providers to be able to throw exceptions to signal that something is not working, and maybe the method might call RuntimeException. In each case, the code that calls these methods must handle the failure and continue. Just because 1 provider drops NPE, I don't want the system to crash. Instead of leaving this to the person calling the method (or, indeed, keeping the line), I would like to make sure that every call will catch all exceptions, declaring each method as:

void x() throws Exception;

but this is usually a bad practice (PMD is not like and usually I agree with the rule for specific methods), so I wonder if this is an exception to the rule or is there a better way?

Let me clarify that I'm looking for a solution in which the calling interface is forced to handle all exceptions (including RuntimeExceptions).

, OSGi. , OSGi , . , , - OSGi, . , , NPE . , , , .

+5
7

, .. MySeviceException . , , , . , .

class MySeviceException extends Exception{
    public MySeviceException() {}  
    public MySeviceException(String msg) { super(msg); }  
    public MySeviceException(Throwable cause) { super(cause); }  
    public MySeviceException(String msg, Throwable cause) { super(msg, cause); } 
}

interface A
{
    void x() throws MySeviceExceptionException;
    void y() throws MySeviceExceptionException;
}

, Errors, Exceptions !

+7

, , RuntimeException , . , throws .

, , try/catch. , , - Javadocss.

, java.lang.Exception; . throws; try/catch ; .

+2

- , . , , .

- , , -.

+1

throws, , . , NPE. , , , - , NPE . , NPE.

"throws Exception" - , . , Struts JUnit, " " , . (JUnit , .)

, , , , Struts. , , , . , -, API- , , - , .

+1

PMD - , Exception, . ,

  • , ( , , ) ,
  • . , , , . , , , , , (. ).

Java . , NullPointerException ex File f , , , , new FileParsingException("error parsing " + f + ": " + ex.getMessage(), ex);

+1

JVM RuntimeException, . / RuntimeExceptions . , , RuntimeExceptions , - , . , RuntimeExceptions (, ) , , , ... - , .

. Sun/Oracle

http://download.oracle.com/javase/tutorial/essential/exceptions/runtime.html

, , Exception, , Exception, Exception, , , , ? Exception, , , , , .

+1

You can implement your interface in an abstract class that uses the method template template to catch and transport RuntimeExceptions with a custom exception. However, there is no way to ensure that providers use an abstract class (other than documentation).

class MySeviceException extends Exception{
    public MySeviceException() {}  
    public MySeviceException(String msg) { super(msg); }  
    public MySeviceException(Throwable cause) { super(cause); }  
    public MySeviceException(String msg, Throwable cause) { super(msg, cause); } 
}

interface A
{
    void x() throws MySeviceExceptionException;
    void y() throws MySeviceExceptionException;
}

class ABase implements A
{
    public final void x() throws MySeviceExceptionException {
        try {
            doX();
        } catch(RuntimeException ex) {
            throw new MySeviceExceptionException(ex);
        }
    }
    public final void y() throws MySeviceExceptionException {
        try {
            doY();
        } catch(RuntimeException ex) {
            throw new MySeviceExceptionException(ex);
        }
    }

    public abstract void doX() throws MySeviceExceptionException;
    public abstract void doY() throws MySeviceExceptionException;
}
+1
source

All Articles