Use a switch statement rather than many ifs
Modernize and refactor exception testing.