Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interface implementation launches different exceptions

I have an interface

public interface DataDAO {
     public void doSomething() throws Exception;
}

Lets say that there are two implementations, one that uses Database to get the data and another one that uses a Webservice.

public class DataDAOJdbc implements DataDAO {
    public void doSomething() throws Exception {
         //Implement
    }
}

public class DataDAOWebService implements DataDAO {
    public void doSomething() throws Exception {
         //Implement
    }
}

As you can already see, the problem is the super generic exception launched. As both implementations need to raise the same kind of exception.

The Jdbc implementation really only raises lets say SQLException while the Webservice implementation only rises the IOException.

Question is, how can I make the interface more elegant, so I capture a proper exception?

The first thing that I though was creating my own exception, and declare it on the interface level

public interface DataDAO {
  public void doSomething() throws MyCoolException;
}

And then, of course implement accordinly.

Question is, does this make sense? I have never created my own exceptions, so I am not really sure if this makes much sense or not. Also, what should I take into account when creating MyCoolException?

like image 208
Juan Antonio Gomez Moriano Avatar asked May 16 '13 05:05

Juan Antonio Gomez Moriano


People also ask

Can exceptions implement interfaces?

Exceptions can be top-level classes, that is, they can have member variables, methods and constructors, they can implement interfaces, and so on.

What is exception interface?

The Exception Interface specifies an exception or error that occurred in a program. An event may contain one or more exceptions in an attribute named exception .

Do we need to override all methods of interface?

Yes, it is mandatory to implement all the methods in a class that implements an interface until and unless that class is declared as an abstract class.

How can we avoid implementing all methods interface?

How can we avoid implementing all methods interface in Java? Declare the missing methods abstract in your class. This forces you to declare your class abstract and, as a result, forces you to subclass the class (and implement the missing methods) before you can create any objects.


1 Answers

The first thing that I though was creating my own exception, and declare it on the interface level (...) does this make sense?

Yes, it does makes sense and I would think it is the best way to handle these situations.

I'll provide a kickoff example for this (based on your current code):

public class MyCoolException extends Exception {
    public MyCoolException() {
    }
    public MyCoolException(String message) {
        this.message = message;
    }
}

public interface DataDAO {
    public void doSomething() throws MyCoolException;
}

public class DataDAOJdbc implements DataDAO {
    public void doSomething() throws MyCoolException {
         //Implement
         try {
         } catch (SQLException e) {
             //handle the exception
             logger.error("Error!", e);
             //throw your custom exception
             throw new MyCoolException(e.getMessage());
         }
    }
}

public class DataDAOWebService implements DataDAO {
    public void doSomething() throws MyCoolException {
         //Implement
         try {
         } catch (IOException e) {
             //handle the exception
             logger.error("Error!", e);
             //throw your custom exception
             throw new MyCoolException(e.getMessage());
         }
    }
}
like image 136
Luiggi Mendoza Avatar answered Oct 29 '22 19:10

Luiggi Mendoza