Introduction to I/O and Networking Flashcards
Exception
An exception is an object of type Exception
Checked exceptions
Compiler-checked exceptions. All other exceptions besides RuntimeException must be check
“finally” keyword
Where you put code that must run regardless of an exception
If the try block fails (an exception)
Flow control immediately moves to the catch block. When the catch block completes, the finally block runs, When the finally block completes, the rest of the method continues on
If the try block succeeds (No exception)
Flow control skips over the catch block and moves to the finally block. When the finally block completes, the rest of the method continues on
If the try or catch block has a return statement, finally will still run
Flow jumps to the finally, then back to the return
A method can throw more than one exception?
Yes, if it darn well needs to. But a method’s declaration must declare all the checked exceptions it can throw
Exception are polymorphic
Exceptions are objects. The benefit for exceptions is that a method doesn’t have to explicitly declare every possible exception it might throw
Exception rules
- You cannot have a catch or finally without a try
- A try must be followed by either a catch or a finally
- You cannot put code between the try and the catch
- A try with only a finally (no catch) must still declare the except
Serialization
Write a file that holds flattened objects. Then have your program read the serialized objects from the file and inflate them back into living, breathing, heap-inhabiting objects
The mother of all catch argument is type Exception
It will catch any exception, including runtime exceptions, so you probably won’t use it outside of testing
Fundamental I/O tehcnique
- Write some data to something
2. Read some data from either a file on disk or a network connection
Connection
Streams represent a connection to a source or destination (file, socket) while chain streams can’t connect on their own and must be chained to a connection stream
Serialization saves the entire object graph
All objects referenced by instance variables, starting with the object being serialized
Serializable interface
Known as a marker or tag interface, because the interface doesn’t have any methods to implement