Skip to content

Latest commit

 

History

History
39 lines (30 loc) · 1007 Bytes

File metadata and controls

39 lines (30 loc) · 1007 Bytes

Exceptions

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.

Exception classes

Throwing

throw new Exception("bad stuff happened");

Try / catch

When we know the code migh generate an Exception:

try {
    ...
} catch(Exception e) {
}
finally {
}

Declaring that you throw an exception

int foo() throws Exception {
}

The compiler ensures that you deal with exceptions (except subclasses of RuntimeException)

Defining your own

Just extend Exception