Implementing an interface raises various exceptions

I have an interface

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

Suppose there are two implementations, one of which uses a database to retrieve data and the other 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 see, the problem is creating a super general exception. Since both implementations should raise the same kind of exception.

The Jdbc implementation really only raises the possibility of a SQLException, while the Webservice implementation only raises an IOException.

The question is, how can I make the interface more elegant, so I am fixing the correct exception?

The first thing, although I created my own exception, and declare it at the interface level

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

And then, of course, implement it.

The question is, does this make sense? I never made my own exceptions, so I'm not sure if this makes sense or not. Also, what should I consider when creating a MyCoolException?

+6
source share
3 answers

The first thing, although I created my own exception, and declare it at the interface level (...), does this make sense?

Yes, that makes sense, and I think this is the best way to handle these situations.

I will give an example to run (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()); } } } 
+4
source

You can use a generic type to define an abandoned interface:

 public interface DataDAO<E extends Throwable> { public void doSomething() throws E; } 

Then your Implementations will look like this:

 public class DataDAOJdbc implements DataDAO<JDBCException> { public void doSomething() throws JDBCException { //Implement } } public class DataDAOWebService implements DataDAO<WebServiceException> { public void doSomething() throws WebServiceException { //Implement } } 

However, this has the disadvantage that you can no longer handle all exceptions in the same way unless you catch an Exception (which pretty much negates the whole point).

+2
source

it makes sense?

Yes Yes. doSomething announcing that doSomething throws a specific checked exception, you signal the callers of the method that they only need to catch and handle that particular exception. By declaring a simple throws Exception , callers will be asked to catch and handle all Exception s, which even include runtime exceptions, such as NullPointerException .

What should I consider when creating a MyCoolException?

It could be that simple:

 public final class MyCoolException extends Exception { public MyCoolException(Throwable cause) { super(cause); } } 

So, your usual exception will simply act as a wrapper to eliminate the cause, whatever it may be. If possible, you can add a message with additional information that may be useful for debugging. When a MyCoolException caught, you can deploy it by calling getCause() , or pass it to a call to the logging framework (its stack will include a reason exception).

+1
source

Source: https://habr.com/ru/post/945123/


All Articles