Exceptions, Iterators, Iterables
Uncaught Exceptions
- When exception thrown, descends call stack (stack of methods that gets called) if not caught
- If exceptions reach bottom of stack, program crashes & Java provides message for user
Checked vs Unchecked Exceptions
- Checked exceptions must be handled
- Compiler requires these to be "caught"/"specified"
- Goal = prevent avoidable program crashes that could have been handled
- Compiler requires these to be "caught"/"specified"
- Any subclass of
RuntimeException
orError
is unchecked - All other
Throwable
s are checked - Checked exceptions = part of specification of class
Checking Exceptions
- Specify method as dangerous w/
throws
keyword- Defers to someone else to handle exception
- If method uses a dangerous method (i.e. might throw checked exception), it becomes dangerous itself
Iteration Intro
- Enhanced for loop/foreach loop = equivalent to iterating through
Iterator
Implementing Iterators
List
interfaceextends
Collection
extends
Iterable
interface → inherits abstractiterator()
method- Instantiate non-static nested class (inner class) → use
instance.new
Iteration Summary
- Implement
Iterable
interface to support enhanced for loopiterator()
method must return object that implementsIterator
interface