title: How to control exception while developing RESTful API with java?
body: <p>I'm developing RESTful API for our clients.<br>
  I'm about to show error information if some error occurs.<br>
  The error information protocol look like below.  </p>
  
  <pre><code>{ 
      "status": "failure",
      "error": {
          "message": "", 
          "type": "",
          "code": 0000 
      } 
  } 
  </code></pre>
  
  <p>In programming level, how to controll exceptions?<br>
  Now I've made custom exception class extending <code>Exception</code> class. (not RuntimeException)<br>
  Is this approach good or not? Is it better using RuntimeExcepion?<br>
  My custom exception class is ...  </p>
  
  <pre><code>public class APIException extends Exception {
      public enum Code {      
          // duplicated exceptions
          ALREADY_REGISTERED(1001),
  
          // size exceptions
          OVER_KEYWORD_LIMIT(2001),
          OVER_CATEGORY_LIMIT(2002),
          TOO_SHORT_CONTENTS_LENGTH(2003),
          TOO_SHORT_TITLE_LENGTH(2004),
  
          // database exceptions
          DB_ERROR(3001),
  
          // unregistered exceptions
          UNREGISTERED_NAME(4001),
  
          // missing information exceptions
          MISSING_PARAMETER(5001),
  
          // invalid information exceptions
          INVALID_PARAMETER(6001),
          INVALID_URL_PATTERN(6002);
  
          private final Integer value;
          private Code(Integer value) {
              this.value = value;
          }
          public Integer getType() {
              return value;
          }
      }
  
      private final Code code;
  
      public APIException(Code code) {
          this.code = code;
      }
      public APIException(Code code, Throwable cause) {
          super(cause);
          this.code = code;
      }
      public APIException(Code code, String msg, Throwable cause) {
          super(msg, cause);
          this.code = code;
      }
      public APIException(Code code, String msg) {
          super(msg);
          this.code = code;
      }
  
      public Code getCode() {
          return code;
      }
  }
  </code></pre>
  
  <p>And using APIException class like this...  </p>
  
  <pre><code>public void delete(int idx) throws APIException {
      try {
          Product product = productDao.findByIdx(idx);
          if (product.getCount() &gt; 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());
      }
  }
  </code></pre>
  
  <p>Which is better make custom exception class or using exist exception like illegalargumentexception..<br>
  If making custom exception class is better, what should I extend among Exception or RuntimeException?<br>
  Please recommend me good example like my situation.<br>
  Thanks in advance.</p>