Exceptions

When writing programs, it’s good to think about all the situations where things could go wrong. For example, if you ask the user to input an integer, you should probably check to see if they actually did.

Java has a special class called Exception that is used when things go wrong in a program. If you reach a certain error state (such as getting bad input), you can throw an exception that describes the error. You can also try troublesome code, and then catch (and handle) any problems that might have happened.