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