8

I am coding a Java Library that will be used to access a DB. I am throwing the exceptions to the end-programmer who uses the JAR library to handle it the way he/she wants.

I wrote a custom Exception (provided below) to wrap connection specific exceptions together so the end-programmer will not have to catch all these exceptions in his code. (to make it easy for him)

is this a good practice when it comes to coding Java libraries? By using this the user will only have to catch NConnectionException in his code.

public class NConnectionException extends Exception {
private static final Logger logger = LoggerFactory.getLogger(NConnectionException.class);
public NConnectionException(Exception e) {

    if (e instanceof NullPointerException) {
        logger.error("ERROR IN READING DF");
        e.printStackTrace();
    }

    else if (e instanceof FileNotFoundException) {
        logger.error("FILE NOT FOUND");
        e.printStackTrace();

    } else if (e instanceof ParserConfigurationException)
    {
        logger.error("PARSE CONF ERR");
        e.printStackTrace();

    }
    else if (e instanceof org.xml.sax.SAXException)
    {
        logger.error("SAX ERR");
        e.printStackTrace();

    }
    else if (e instanceof IOException)
    {
        logger.error("IO ERR");
        e.printStackTrace();

    }

}

}

ndnine89
  • 137
  • 2
  • 3
  • 12
  • 1
    In your case your are not storing any stack trace or error message in your custom exception. Using `e.printStackTrace();` is a bad practice. – Naman Gala Apr 09 '15 at 12:24
  • You can store multiple exception in a list inside your `NConnectionException`, and let your calling program decide what data it require from the exception. I would suggest not to handle runtime exception i.e. unchecked exceptions. – Naman Gala Apr 09 '15 at 12:27
  • And by doing so I have to iterate through the exception list and find which exception that is thrown? Or haven't I understood what you said correctly? Your help is much appreciated! – ndnine89 Apr 10 '15 at 05:02
  • And instead of printing the stack trace I threw the exception object. Is that encouraged? – ndnine89 Apr 10 '15 at 05:04
  • Yes, calling program need to iterate through it. Does your program throws multiple exceptions `at a time`? As this will occur only in multithreaded environment. – Naman Gala Apr 10 '15 at 05:06
  • Yes, It can. Will that be a problem? – ndnine89 Apr 10 '15 at 05:07
  • I mean to say, does your program throws `NullPointerException` as well as `FileNotFoundException` for a single call? Or it will throw one of them in a single call? – Naman Gala Apr 10 '15 at 05:09
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/74893/discussion-between-ndnine89-and-naman-gala). – ndnine89 Apr 10 '15 at 05:10

2 Answers2

3

You can pass a cause (Throwable) to a custom exception. Look at the Exception javadoc for more Information.

Edit:

public class CustomException extends Exception {
    public CustomException(Throwable t) {
        super(t);
    }
}

public void testMethod(String s) throws CustomException {
   try {
       int integer = Integer.parseInt(s);
   } catch (NumberFormatException e) {
       throw new CustomException(e);
   }
}

try {
    testMethod("not a number");
} catch (CustomException ce) {
    ce.printStackTrace(); // this will print that a CustomException
                          // with the cause NumberFormatException has occured.
    ce.getCause(); // this will return the cause that
                   // we set in the catch clause in the method testMethod
}
Rene8888
  • 199
  • 2
  • 12
  • As per your suggestion I used this constructor , public Exception(String message,Throwable cause) and threw the cause to my customException. I am unclear what the next step is. Would you please elaborate. – ndnine89 Apr 10 '15 at 04:59
  • I added a example for you – Rene8888 Apr 10 '15 at 07:28
2

According to this post, wrapping all the exceptions in a single is not good.


If you want to wrap them then

As your program will throw only one exception at a time then no need to store list of exceptions in NConnectionException.

And you can create a single object of exception in NConnectionException class. You can refer this structure.

And store the thrown exception in that object and throw back newly created object of NConnectionException class. Let the calling program catch NConnectionException exception and take out the stored object and act accordingly.

Note : Generally we don't handle unchecked exception (like NullPointerException), calling program will take care of it.

Community
  • 1
  • 1
Naman Gala
  • 4,670
  • 1
  • 21
  • 55