001    /*****************************************************************************
002     *                                                                           *
003     *  This file is part of the BeanShell Java Scripting distribution.          *
004     *  Documentation and updates may be found at http://www.beanshell.org/      *
005     *                                                                           *
006     *  Sun Public License Notice:                                               *
007     *                                                                           *
008     *  The contents of this file are subject to the Sun Public License Version  *
009     *  1.0 (the "License"); you may not use this file except in compliance with *
010     *  the License. A copy of the License is available at http://www.sun.com    * 
011     *                                                                           *
012     *  The Original Code is BeanShell. The Initial Developer of the Original    *
013     *  Code is Pat Niemeyer. Portions created by Pat Niemeyer are Copyright     *
014     *  (C) 2000.  All Rights Reserved.                                          *
015     *                                                                           *
016     *  GNU Public License Notice:                                               *
017     *                                                                           *
018     *  Alternatively, the contents of this file may be used under the terms of  *
019     *  the GNU Lesser General Public License (the "LGPL"), in which case the    *
020     *  provisions of LGPL are applicable instead of those above. If you wish to *
021     *  allow use of your version of this file only under the  terms of the LGPL *
022     *  and not to allow others to use your version of this file under the SPL,  *
023     *  indicate your decision by deleting the provisions above and replace      *
024     *  them with the notice and other provisions required by the LGPL.  If you  *
025     *  do not delete the provisions above, a recipient may use your version of  *
026     *  this file under either the SPL or the LGPL.                              *
027     *                                                                           *
028     *  Patrick Niemeyer (pat@pat.net)                                           *
029     *  Author of Learning Java, O'Reilly & Associates                           *
030     *  http://www.pat.net/~pat/                                                 *
031     *                                                                           *
032     *****************************************************************************/
033    
034    
035    package bsh;
036    
037    /**
038            UtilEvalError is an error corresponding to an EvalError but thrown by a 
039            utility or other class that does not have the caller context (Node) 
040            available to it.  A normal EvalError must supply the caller Node in order 
041            for error messages to be pinned to the correct line and location in the 
042            script.  UtilEvalError is a checked exception that is *not* a subtype of 
043            EvalError, but instead must be caught and rethrown as an EvalError by 
044            the a nearest location with context.  The method toEvalError( Node ) 
045            should be used to throw the EvalError, supplying the node.
046            <p>
047    
048            To summarize: Utilities throw UtilEvalError.  ASTs throw EvalError.
049            ASTs catch UtilEvalError and rethrow it as EvalError using 
050            toEvalError( Node ).  
051            <p>
052    
053            Philosophically, EvalError and UtilEvalError corrospond to 
054            RuntimeException.  However they are constrained in this way in order to 
055            add the context for error reporting.
056    
057            @see UtilTargetError
058    */
059    public class UtilEvalError extends Exception 
060    {
061            protected UtilEvalError() {
062            }
063    
064            public UtilEvalError( String s ) {
065                    super(s);
066            }
067    
068            /**
069                    Re-throw as an eval error, prefixing msg to the message and specifying
070                    the node.  If a node already exists the addNode is ignored.
071                    @see #setNode( bsh.SimpleNode )
072                    <p>
073                    @param msg may be null for no additional message.
074            */
075            public EvalError toEvalError( 
076                    String msg, SimpleNode node, CallStack callstack  ) 
077            {
078                    if ( Interpreter.DEBUG )
079                            printStackTrace();
080    
081                    if ( msg == null )
082                            msg = "";
083                    else
084                            msg = msg + ": ";
085                    return new EvalError( msg+getMessage(), node, callstack );
086            }
087    
088            public EvalError toEvalError ( SimpleNode node, CallStack callstack ) 
089            {
090                    return toEvalError( null, node, callstack );
091            }
092    
093    }
094