Exceptions

From Jonathan Gardner's Tech Wiki
Jump to: navigation, search

What is this code supposed to do?

int("asdf")

If it returns a number, then something is wrong.

There are roughly three responses a function can give:

  1. The answer you expect.
  2. Something else, indicating that the answer can't be given.
  3. An error, since something has gone horribly wrong somewhere.

Exceptions are a great way to encapsulate the result for when the answer can't be given, either due to an error or something else.

Exceptions do not have to be unexpected. You can write code that expects exceptions.

Exceptions do not have to be errors. It can be completely normal for your code to throw and exception.

See Also