Thursday, April 11, 2013

Create User Defined Exception


I hope that the following snippet will give you a better understanding how  to create the User defined exception using java

1) FinanceExceptio.java
package com.sample.utility.exceptions;

/**
 * The class <code>FederalBank</code> and its subclasses are a form of
 * <code>Exception</code> that indicates conditions that a reasonable
 * application might want to catch.
 *
 * @author SunilG
 * @version 1.0, 11 April 2013
 */
public class FinanceException extends Exception {
    /**
     * The serial version id.
     */
    private static final long serialVersionUID = 1878273194442401814L;
    /**
     * The cached root exception wrapped and sent by the subclasses.
     */
    private Throwable root;

    /**
     * Constructs a new exception with the specified detail message.
     *
     * @param message
     *            the detail message. The detail message is saved for later
     *            retrieval by the {@link #getMessage()} method.
     */
    public FinanceException(final String message) {
        super(message);
    }

    /**
     * Constructs a new exception with the specified detail
     * message and cause.
     *
     * @param message
     *            the detail message (which is saved for later
     *            retrieval by the {@link #getMessage()} method).
     * @param throwable
     *            the cause (which is saved for later retrieval by the
     *            {@link #getCause()} method).
     */
    public FinanceException(final String message,
    final Throwable throwable) {
        this(message);
        this.root = throwable;
    }

    /**
     * Constructs a new exception with the specified cause.
     *
     * @param throwable
     *            the cause (which is saved for later retrieval by the
     *            {@link #getCause()} method).
     */
    public FinanceException(final Throwable throwable) {
        this.root = throwable;
    }

    /**
     * Returns the root cause of this Exception.
     *
     * @return the cause of this Exception or <code>null</code>
     * if the cause is nonexistent or unknown.
     */
    public Throwable getRootCause() {
        return this.root;
    }
}

2) BusinessException.java

package com.sample.utility.exceptions;

/**
 * @author SunilG
 *
 */
public class BusinessException extends FinanceException {

    /**
     * The serial version id.
     */
    private static final long serialVersionUID = 6527473722381576450L;

    /**
     * Constructs a new exception with the specified detail message.
     *
     * @param message
     *            the detail message. The detail message is saved for later
     *            retrieval by the {@link #getMessage()} method.
     */
    public BusinessException(final String message) {
        super(message);
    }

    /**
     * Constructs a new exception with the specified cause.
     *
     * @param cause
     *            the cause (which is saved for later retrieval by the
     *            {@link #getCause()} method).
     */
    public BusinessException(final Throwable cause) {
        super(cause);
    }

    /**
     * Constructs a new exception with the specified detail message and cause.
     *
     * @param message
     *            the detail message (which is saved for later retrieval by the
     *            {@link #getMessage()} method).
     * @param cause
     *            the cause (which is saved for later retrieval by the
     *            {@link #getCause()} method).
     */
    public BusinessException(final String message,
    final Throwable cause) {
        super(message, cause);
    }
}

3) DBException.java

package com.sample.utility.exceptions;

import java.sql.SQLException;

import org.hibernate.HibernateException;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataAccessResourceFailureException;

/**
 * The class <code>DBException</code> and its subclasses are a form of
 * <code>FederalBankException</code> that indicates exception conditions that
 * occur in the data access layer.
 *
 * <p>
 * Applications should throw instances of this class to indicate other issues
 * related Database operation

 * @author SUNILG
 * @version 1.0, 11 April 2013
 */
public class DBException extends FinanceException {

    /**
     * The serial version id.
     */
    private static final long serialVersionUID = 2696949392657790519L;

    /**
     * Constructs a new exception with the specified detail message.
     *
     * @param message
     *            the detail message. The detail message is saved for later
     *            retrieval by the {@link #getMessage()} method.
     */
    public DBException(final String message) {
        super(message);
    }

    /**
     * Constructs a new exception with the specified cause.
     *
     * @param cause
     *            the cause (which is saved for later retrieval by the
     *            {@link #getCause()} method).
     */
    public DBException(final SQLException cause) {
        super(cause);
    }

    /**
     * Constructs a new exception with the specified detail message and cause.
     *
     * @param message
     *            the detail message (which is saved for later retrieval by the
     *            {@link #getMessage()} method).
     * @param cause
     *            the cause (which is saved for later retrieval by the
     *            {@link #getCause()} method).
     */
    public DBException(final String message,
    final SQLException cause) {
        super(message, cause);
    }

    /**
     * Constructs a new exception with the specified cause.
     *
     * @param cause
     *            the cause (which is saved for later retrieval by the
     *            {@link #getCause()} method).
     */
    public DBException(final HibernateException cause) {
        super(cause);
    }

    /**
     * Constructs a new exception with the specified detail message and cause.
     *
     * @param message
     *            the detail message (which is saved for later retrieval by the
     *            {@link #getMessage()} method).
     * @param cause
     *            the cause (which is saved for later retrieval by the
     *            {@link #getCause()} method).
     */
    public DBException(final String message,
    final HibernateException cause) {
        super(message, cause);
    }

    /**
     * Constructs a new exception with the specified detail message and cause.
     *
     * @param message
     *            the detail message (which is saved for later retrieval by the
     *            {@link #getMessage()} method).
     * @param cause
     *            the cause (which is saved for later retrieval by the
     *            {@link #getCause()} method).
     */
    public DBException(final String message,
    final Throwable cause) {
        super(message, cause);
    }

    /**
     * Constructs a new exception with the specified cause.
     *
     * @param cause
     *            the cause (which is saved for later retrieval by the
     *            {@link #getCause()} method).
     */
    public DBException(final DataAccessException cause) {
        super(cause);
    }

    /**
     * Constructs a new exception with the specified detail message and cause.
     *
     * @param message
     *            the detail message (which is saved for later retrieval by the
     *            {@link #getMessage()} method).
     * @param cause
     *            the cause (which is saved for later retrieval by the
     *            {@link #getCause()} method).
     */
    public DBException(final String message,
    final DataAccessException cause) {
        super(message, cause);
    }

    /**
     * Constructs a new exception with the specified cause.
     *
     * @param cause
     *            the cause (which is saved for later retrieval by the
     *            {@link #getCause()} method).
     */
    public DBException(final DataAccessResourceFailureException cause) {
        super(cause);
    }

    /**
     * Constructs a new exception with the specified detail message and cause.
     *
     * @param message
     *            the detail message (which is saved for later retrieval by the
     *            {@link #getMessage()} method).
     * @param cause
     *            the cause (which is saved for later retrieval by the
     *            {@link #getCause()} method).
     */
    public DBException(final String message,
    final DataAccessResourceFailureException cause) {
        super(message, cause);
    }

    /**
     * Constructs a new exception with the specified cause.
     *
     * @param cause
     *            the cause (which is saved for later retrieval by the
     *            {@link #getCause()} method).
     */
    public DBException(final IllegalStateException cause) {
        super(cause);
    }

    /**
     * Constructs a new exception with the specified detail message and cause.
     *
     * @param message
     *            the detail message (which is saved for later retrieval by the
     *            {@link #getMessage()} method).
     * @param cause
     *            the cause (which is saved for later retrieval by the
     *            {@link #getCause()} method).
     */
    public DBException(final String message,
    final IllegalStateException cause) {
        super(message, cause);
    }
}


No comments:

Post a Comment