aviad
aviad

Reputation: 8278

Exception in method signature in interface

I have the following interface:

public interface ICalculationRule {
    public void calculate(EventBag eventBag);
}

and I would like to provide some way to indicate if calculation failed, what would be more correct?

Upvotes: 0

Views: 4423

Answers (5)

C.c
C.c

Reputation: 1935

Generally speaking, we don't recommend that using exception to handle logical procedure. an exception should be treated as a real exception and only occurred when it have to. To throw an exception, it absolutely spends more time than that just simply return a value, also, I believe that correctly flag a meaningful returned value would be more make sense for code reader and adopter in future. But, this is unavoidable, in case of somewhere indicated hardy processing by return a value, you can use throw exceptions.

For your case, the following option should be a correct choice.

  make calculate method boolean (true=success, false - failure)

Upvotes: 0

Rohit Sharma
Rohit Sharma

Reputation: 13815

It all depends on

 " Is the error is expected or unexpected ? "
  • add throws Exception to method signature

If during calculation something unexpected happens. It is suppose to throw Exception. If it is supposed to be handled within use try catch And if caller of your method is suppose to react on that. Then add throws Exception.

  • make calculate method boolean (true=success, false - failure)

If Result NO is something expected to happen and If error conditions are not of that significance and caller is just interested in knowing the result of sucess or failure. Go for it.

Also don't go For Generic Exception But be specific.

Also i can think of making delegates to notify caller about success or failure.

public interface Response {
   public void onSuccess();
   public void onFailure(Exception exception);  
} 

I can seek its implementation object by the caller and in calculation method call. And on success or failure can invoke respective delegate like

public void calculation(Response response) { 
    Exception e = null;

    try {
        // Do something here
    } catch (IOException ioe) {
        e = ioe;
    }

    if(e == null) {
        response.onSuccess();
    } else {
        response.onFailure(e);
    }
}

hope this guide you to the right dirction :)

Upvotes: 2

frictionlesspulley
frictionlesspulley

Reputation: 12358

  1. You could try throwing a customized Exception which inherts from RuntimeException for an error and a result , thus the client of the interface does not have to explicitly. This helps to avoid unwanted try catch blocks

  2. Instead of a boolean response you could try sending a Result object which would capture both success and failure, there gaining a container to pass information between the caller and callee

Upvotes: 0

yair
yair

Reputation: 9245

It's important to understand that declaring an Exception may be thrown, indicates that it has to be treated by the client code. It means that you, as the author of the method, cannot just let it pass.

On the other hand, if the calculation failure is something not that acute, something that maybe a problem for the client code and also may not, you can just return boolean as you stated, letting the client code decide whether a special treatment should apply. If you think the client needs more information than just a boolean you can always return some sort of CalculationResult object.

Upvotes: -1

Alex Gitelman
Alex Gitelman

Reputation: 24722

Exception has an advantage of caring more information than just simple boolean answer. If all you ever need is yes/no answer - return boolean as it will make code slightly more concise but if you need more details throw exception.

But make sure to have your own exception rather that standard java.lang.Exception. It's always bad and double bad in interface.

Upvotes: 1

Related Questions