I am developing a RESTful API for our clients.
I am going to show error information if some kind of error occurs.
The error information log looks below.
{ "status": "failure", "error": { "message": "", "type": "", "code": 0000 } }
At the programming level, how to handle exceptions?
Now I have created my own exception class that extends the Exception class. (not a RuntimeException)
Is this approach good or not? Is it better to use RuntimeExcepion?
My custom exception class ...
public class APIException extends Exception { public enum Code {
And using an APIException class like this ...
public void delete(int idx) throws APIException { try { Product product = productDao.findByIdx(idx); if (product.getCount() > 0) { throw new APIException(Code.ALREADY_REGISTERED, "Already registered product."); } productDao.delete(idx); } catch (Exception e) { throw new APIException(Code.DB_ERROR, "Cannot delete product. " + e.getMessage()); } }
What is better to make a custom exception class or to use an existing exception such as unlargumentexception ..
If I can make my own exception class better, what should I distribute among the exceptions or the RuntimeException?
Please recommend me a good example, for example, my situation.
Thanks in advance.
source share