i. Grades online – forms page
i. Coming soon – stay tuned
i. Signal that something went wrong
ii. Some examples:
1. ArrayOutOfBoundsException
2. NullPointerException
3. ArithmeticException
i. If something goes wrong, Throw an exception
ii. Either one of the existing ones, or we can
create our own
iii. Syntax:
if(/*Something bad happens*/) {
throw
new <Type of Exception>();}
iv. throw vs throws
i. When an exception is thrown, execution
continues with the appropriate exception handler.
ii. This may or may not be in the same
method/class. It will go in order down
the call stack until a handler is found.
iii. Try/catch
iv. Syntax:
try {
//Code that might cause
exception}
catch(<Type of exception> <object name>)
{
//Do something, such as:
//<objectName>.printStackTrace();}
i. Errors
1. Out of our control – eg out of memory
ii. Unchecked Exceptions
1. Runtime exceptions that we should have
prevented
iii. Checked Exceptions
1. Exceptions that we can’t foresee, but we must
handle
i. ()
ii. (String message)
i. getMessage()
1. Returns the message stored in the exception
ii. printStackTrace()
1. prints out current call stack at time of
exception
i. Error.java
1. OutOfMemoryError
2. Can’t catch…
ii. UncheckedException.java
1. ArithmeticException and InputMismatchException
2. In general, you should prevent these sorts of
errors, not handle them
3. Note – we can have multiple catches for one
try. They are evaluated top to bottom
iii. CheckedException.java
1. FileNotFoundException
i. Exception is thrown in called method
ii. Then, it is caught in the calling method
iii. ExceptionsExample.java
i. Will catch any exceptions
i. Note – this can only happen with unchecked
exceptions, or if we keep saying that a method throws an exception (without
handling it)