package edu.princeton.toy.lang; /** * TDefaultExceptionHandler is the simplest mutable implementation of TExceptionHandler. * * @author btsang * @version 7.1 */ public class TDefaultExceptionHandler implements TExceptionHandler { private boolean willThrow[]; /** * Instantiates a new TDefaultExceptionHandler in an exceptionless state. */ public TDefaultExceptionHandler() { willThrow = new boolean[TExceptionType.TYPES.length]; } /** * Throws a TException with the specified type iff the specified exception was not set to be * ignored. The Virtual Machine should raise all exceptional conditions and let the handler * decide wheter or not that exception should be propagated through the rest of the machine. * * @param type The type of exceptional condition that was raised in the Virtual Machine. */ public void raise(TExceptionType type) throws TException { if (willThrow[type.hashCode()]) throw new TException(type); } /** * Gets wheter or not the handler should throw a raised exception of the specified type. * * @param index The index of type (in the TExceptionType.TYPES array) in question. An invalid * value will result in an ArrayIndexOutOfBoundsException. * @return True iff the handler will throw exceptions of this type. * @see edu.princeton.toy.lang.TExceptionType#TYPES */ public boolean getWillThrow(int index) { return willThrow[index]; } /** * Gets wheter or not the handler should throw a raised exception of the specified type. * * @param type The type in question. A null value will result in a NullPointerException. * @return True iff the handler will throw exceptions of this type. * @see edu.princeton.toy.lang.TExceptionType#TYPES */ public boolean getWillThrow(TExceptionType type) { return willThrow[type.hashCode()]; } /** * Sets wheter or not the handler should throw a raised exception of the specified type. * * @param type The type in question. A null value will result in a NullPointerException. * @param willThrow Wheter or not the handler should throw exceptions of this type. * @see edu.princeton.toy.lang.TExceptionType#TYPES */ public void setWillThrow(TExceptionType type, boolean willThrow) { this.willThrow[type.hashCode()] = willThrow; } /** * Sets wheter or not the handler should throw a raised exception of the specified type. * * @param index The index of type (in the TExceptionType.TYPES array) in question. An invalid * value will result in an ArrayIndexOutOfBoundsException. * @param willThrow Wheter or not the handler should throw exceptions of this type. * @see edu.princeton.toy.lang.TExceptionType#TYPES */ public void setWillThrow(int index, boolean willThrow) { this.willThrow[index] = willThrow; } }