package tcra.math;

/**
 * MathFormatException is thrown by MathTypeParsers when it discovers
 * it is parsing a malformed String format. It wraps any other 
 * Exception that may be thrown during parsing as well.
 * <p>
 * Based on SAXException at:
 * <link>http://www.openhealth.org/RDDL/org/xml/sax/SAXException.java
 * </link>
 */
@SuppressWarnings("serial")
public class MathFormatException extends Exception
{
   // Exception that can be wrapped by ours
   private Exception exception;

   /**
    * Default constructor. Does not contain any wrapped exception.
    * No custom error message.
    */
   public MathFormatException()
   {
      super();
      exception = null;
   }

   /**
    * Constructor specifying message to be printed. Does not wrap
    * a sub-exception.
    *
    * @param message Custom error message that will be printed to
    *                user.
    */
   public MathFormatException(String message)
   {
      super(message);
      exception = null;
   }

   /**
    * Constructor specifying the exception to be wrapped. The
    * exception comes from some other function that throws an error.
    * No custom error message.
    *
    * @param e Exception that occured during parsing from some
    *          other function.
    */
   public MathFormatException(Exception e)
   {
      super();
      exception = e;
   }

   /**
    * Constructor specifying a custom error message and a wrapped
    * exception.
    *
    * @param message Custom error message that will be printed to
    *                the user.
    * @param e       Wrapped exception.
    */
   public MathFormatException(String message, Exception e)
   {
      super(message);
      exception = e;
   }

   /**
    *  Return the exception message. Will first try to return
    *  the message from Exception, then the wrapped exception's
    *  message, and lastly the MathFormatException message.
    *
    *  @return String Exception message.
    */
   public String getMessage()
   {
      String message = super.getMessage();
      if(message == null && exception != null)
         return exception.getMessage();
      else return message;
   } // end getMessage

   /**
    * Return the wrapped exception.
    *
    * @return Exception thrown by some process during parsing.
    */
   public Exception getException()
   {
      return exception;
   } // end getException

   public String toString()
   {
      if(exception != null) return exception.toString();
      else return super.toString();
   } // end toString
} // end class
