CS 111B Lecture Notes - Call Stacks and Exceptions - Chapter 10
Call Stacks and Exceptions
Catching Exceptions
- To catch (handle) an exception, use a try block together with a catch block.
- try is a keyword that begins a block of code in which any exceptions that occur will be
handled. If an exception occurs in a try block, the code jumps out of that try block to the appropriate
catch block.
- catch is a keyword that begins a block of code in which a class of exception is handled. The
code in a catch block is executed only if an exception is thrown from within the immediately preceding
try block. Each catch block specifies a specific exception class. That catch block is only executed if
an Exception of that class (or a subclass of it) is thrown.
- A catch block is much like a function, with a single parameter of the exception object. This
"function" is called automatically when a matching exception is thrown from the try block immediately
preceding it.
- finally is a keyword that begins a block of code after a set of catch blocks, containing code
that is always executed whether or not an exception occurred. The "finally" block is optional when
handling exceptions.
- If an exception is not caught by the function in which it is thrown, then the exception propagates
to the calling function. In other words, exceptions travel down the call stack until they are caught.
If the main function doesn't catch the exception, the program terminates and displays an error message
like we see on our console window.
- Example: RobustInvestApplet.java: a version of the
InvestApplet that handles non-numeric user input.
Throwing Exceptions
- When an exception occurs, we say it is "thrown". The java keyword throw is used to cause an
exception to occur. So if e is a reference variable to an exception object, then
throw e; will throw the exception that e references.
- Throwable is the parent class of both of the following classes:
- Error is the parent class of errors that are so serious that the program must just crash -
these are like exceptions, but they can't be caught or handled.
- Exception is the parent class of all exceptions that can be caught or handled. These come
in two varieties:
- RuntimeException is the parent class of some very common exceptions such as
ClassCastException and NumberFormatException. These exceptions are all preventable by thorough
programming. (Such as checking if a String contains only numeric characters before trying to parse
it into an integer.)
- All exceptions which aren't RuntimeExceptions are called Checked Exceptions - if there is
a possibility they might be thrown, then they need to be caught or declared. These are the kinds of
exceptions that the programmer cannot control, so code must be written to anticipate them. (E.g.
network or file communication problems.)
- throws is a java keyword that can be used at the end of a function header. It is how a
function declares that it might throw an exception. All uncaught Checked Exceptions in a function's body
must be listed after "throws" in the function header. This will become important when we get into file
and keyboard i/o.
- Example: RobustTime.java and
RobustTimeTest.java: a variation of the solution to Practice Problem 3, which defines an exception
class, throws, and catches it.
Return to the main CS 111B page