0

Hello I'm still new to programming and I have recently learned about Exceptions and how it is better to Define a exception than use a generic one. I have researched and I understand the basic idea of Creating exceptions such as using String and Throwable. However when it come to code like this:

public void sendEnvironmentExport(Environment env) throws Exception {

    if (details == null) {
        details = new CurrentUserDetails();
        details.setPrincipal(dao.loadUser(UserServiceImpl.SYSTEM_USERNAME));
    }

    for (String reportDescriptor : env.getExportReports()) {
        // methodName|recipient@address.com
        String[] parts = reportDescriptor.split("\\|");

        if (parts.length == 2) {
            Method m = this.getClass().getMethod(parts[0], String.class);
            m.invoke(this, parts[1]);
        } else {
            logger.warn("Ignoring invalid Environment.getExportReports.report: " + reportDescriptor);
        }
    }

}

I have no clue where to begin with creating and exception. So basically my question is. In these cases is it better to Define and exception or use a generic one?

Sorry if I am asking an unclear question I am simply trying to understand if there is any need to define an exception for methods such as this where there is no clear area if the exception being thrown.

McGhee
  • 13
  • 5

2 Answers2

1

In most situations it's nicer to use a specific exception that extends from the closest possible generic exception, unless there is a predefined Java exception that gives exactly the information you would want to give. The reason for using a custom one is that the exception can be handled in a very specific manner. Even if a method can throw multiple similar but still distinct custom exceptions, one can easily handle each one individually or all at once (by handling the superclass exception).

Andreas Hartmann
  • 1,825
  • 4
  • 23
  • 36
0

There are a number of different strategies for handling exceptions. Some developers use only RuntimeExceptions, some developers use only standard exceptions, some developers define a few general exceptions, say one per tier, and others define exceptions for every possible error. It is really a question of preference and stile.

An exception can be created like this:

public class MyException extends RuntimeException{
    private static final long serialVersionUID = 1L;
}

It can be thrown like this:

throw new MyException();

and cought like this:

try {
    throw new MyException();
} catch (MyException e) {
    e.printStackTrace();
}
Jan Larsen
  • 831
  • 6
  • 13
  • Be careful when you create an Exception which extends `RuntimeException` since this is a [Unchecked Exception](https://docs.oracle.com/javase/tutorial/essential/exceptions/runtime.html) –  Mar 07 '18 at 14:20
  • Yes, RuntimeException is a special case and probably not the best example – Andreas Hartmann Mar 07 '18 at 14:21
  • That was very deliberate. I think checked exceptions were a mistake. They cause way more confusion and bad code than they cause good. Discussion here: https://stackoverflow.com/questions/613954/the-case-against-checked-exceptions – Jan Larsen Mar 07 '18 at 14:47