Fixed foreach not finding variable. Used correct exception.