001    package org.maltparser.core.exception;
002    
003    /**
004     * MaltChainedException handles a chain of MaltParser specific exception. 
005     *
006     * @author Johan Hall
007    **/
008    public class MaltChainedException extends Exception {
009            public static final long serialVersionUID = 8045568022124816379L;
010            private Throwable cause = null;
011    
012            /**
013             * Creates a MaltChainedException instance
014             */
015            public MaltChainedException() {
016                    super();
017            }
018    
019            /**
020             * Creates a MaltChainedException instance with a message
021             * 
022             * @param message a message string
023             */
024            public MaltChainedException(String message) {
025                    super(message);
026            }
027    
028            /**
029             * Creates a MaltChainedException instance with a message and keeps track of the cause of the exception.
030             * 
031             * @param message       a message string
032             * @param cause         a cause
033             */
034            public MaltChainedException(String message, Throwable cause) {
035                    super(message);
036                    this.cause = cause;
037            }
038            
039            
040            /* (non-Javadoc)
041             * @see java.lang.Throwable#getCause()
042             */
043            public Throwable getCause() {
044                    return cause;
045            }
046    
047            /**
048             * Returns a string representation of the exception chain. Only MaltParser specific exception is included.
049             * 
050             * @return a string representation of the exception chain
051             */
052            public String getMessageChain() {
053                    StringBuilder sb = new StringBuilder();
054                Throwable t = this;
055                
056                while (t != null) {
057                  if (t.getMessage() != null && t instanceof MaltChainedException) {
058                      sb.append(t.getMessage()+"\n");
059                  }
060                  t = t.getCause();
061                }  
062                return sb.toString();
063            }
064            
065            /* (non-Javadoc)
066             * @see java.lang.Throwable#printStackTrace()
067             */
068            public void printStackTrace() {
069                    super.printStackTrace();
070                    if (cause != null) {
071                            cause.printStackTrace();
072                    }
073            }
074    }