import java.util.Random;
import java.util.List;
import java.util.ArrayList;
import java.util.NoSuchElementException;

/**
 * Throw an exception chosen randomly from a list.
 *
 * @author Mark Young (A00000000)
 */
public class RandomThrower {

    private static final List<Exception> list = new ArrayList<>();
    private static final Random rand = new Random();

    /**
     * Add an exception to the list.
     *
     * @param except the exception to add
     */
    public static void add(Exception except) {
        list.add(except);
    }

    /**
     * Choose an exception at random.
     *
     * @throws Exception an exception chosen at random from the current list
     */
    public static void randomThrow() 
            throws Exception {
        if (list.isEmpty()) {
            throw new NoSuchElementException("There's nothing in the list!");
        }
        int posn = rand.nextInt(list.size());
        throw list.get(posn);
    }

}
