When are Custom Exceptions worth it?

There is a common practice of making new custom exceptions which extend Exception class. These exceptions are given names in order to make the code readable. Although these make the code readable, but the overhead something one cannot be forgiven for.

To cut the story short, these custom exceptions must be created only if they carry some extra information about the exception. Unless they carry some extra information, Exception object is suffice.

e.g. Lets say there is a certain set of error codes defined for a given API, also there is some error message related to the error code along with a resolution field for the exception.

CustomException for some imaginary API [ErrorCode + ErrorMessage + ErrorResolution]

/**
 * Custom Exception for API with error code, error message and error resolution
 * 
 * @author lekhak.tumblr.com
 */
public class APIException extends Exception {
	private static final long serialVersionUID = -7804182979265816573L;

	private int errorCode;
	private String errorMessage;
	private String errorResolution;

	/**
	 * Exception Default Constructor
	 */
	public APIException() {

	}

	/**
	 * Exception Constructor, sets the error code as default error message
	 * 
	 * @param errorMessage
	 */
	public APIException(String errorMessage) {
		super(errorMessage);
	}

	/**
	 * Complete Exception Constructor
	 * 
	 * @param errorCode
	 * @param errorMessage
	 * @param errorResoultion
	 */
	public APIException(int errorCode, String errorMessage,
			String errorResoultion) {
		this.errorCode = errorCode;
		this.errorMessage = errorMessage;
		this.errorResolution = errorResoultion;
	}

	/**
	 * Returns the error code
	 * 
	 * @return error code
	 */
	public int getErrorCode() {
		return errorCode;
	}

	/**
	 * Sets the error code
	 * 
	 * @param errorCode
	 */
	public void setErrorCode(int errorCode) {
		this.errorCode = errorCode;
	}

	/**
	 * Returns the error message
	 * 
	 * @return String errorMessage
	 */
	public String getErrorMessage() {
		return errorMessage;
	}

	/**
	 * Sets the error message
	 * 
	 * @param errorMessage
	 */
	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}

	/**
	 * Returns the resolution for the error
	 * 
	 * @return
	 */
	public String getErrorResolution() {
		return errorResolution;
	}

	/**
	 * Sets the resoltion of the error
	 * 
	 * @param errorResolution
	 */
	public void setErrorResolution(String errorResolution) {
		this.errorResolution = errorResolution;
	}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: