Many times operations find unexpected errors; we could make every function or method return an object containing both an error condition and the actual value, but this is cumbersome.
Java (and other programming languages) supports Exceptions and exception handling.
- interface Throwable
- class Exception
- class RuntimeException
throw new Exception("bad stuff happened");When we know the code migh generate an Exception:
try {
...
} catch(Exception e) {
}
finally {
}int foo() throws Exception {
}The compiler ensures that you deal with exceptions (except subclasses of RuntimeException)
Just extend Exception