




























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An in-depth exploration of java exceptions and serialization based on the text 'effective java' by joshua bloch. Topics covered include exception handling approaches, propagating exceptions, representing exceptions as objects, and the java exception class hierarchy. Serializability is also discussed, including its definition, uses, and deep copy process.
Typology: Study notes
1 / 36
This page cannot be seen from the preview
Don't miss anything!





























Overview
Motivation Representation in Java
Problem
Not enough information in method / class Need more information to decide action
Decide at application level (instead of library) Examples
Incorrect data format
ask user to reenter data
Unable to open file
ask user for new filename
Insufficient disk space
ask user to delete files
Exception Handling – Exit Program Approach
Exit program with error message / error code
System.err.println(“Error found”);
// message
System.exit(1);
// error code
Drastic solution Event must be handled by user invoking program Program may be able to deal with some exceptions
Exception Handling – Throw Exception
Throw exception (caught in parent’s catch block)
if (error) throw new ExceptionType(); } B( ) {
try {
} catch (ExceptionType e) { ...action... } }
Java exception backtracks tocaller(s) until matching catchblock found
Exception Handling – Throw Exception
Compiler ensures exceptions are caught eventually No need to explicitly propagate exception to caller
Backtrack to caller(s) automatically Class hierarchy defines meaning of exceptions
No need for separate definition of error codes Exception handling code separate & clearly marked
Representing Exceptions
Two types of exceptions
checked & unchecked
Representing Exceptions
Object^ Object
Error^ Error
Throwable^ Throwable
Exception^ Exception
ClassNotFoundException^ ClassNotFoundExceptionCloneNotSupportedException^ CloneNotSupportedExceptionIOException^ IOExceptionAWTException^ AWTExceptionRuntimeException^ RuntimeExceptionLinkageError^ LinkageErrorVirtualMachoneError^ VirtualMachoneErrorAWTError^ AWTError … …
ArithmeticException^ ArithmeticExceptionNullPointerException^ NullPointerExceptionIndexOutOfBoundsException^ IndexOutOfBoundsException
Checked^ CheckedUnchecked^ Unchecked
NoSuchElementException^ NoSuchElementException …
Checked Exceptions
IOException, ClassNotFoundException
Catch and handle exception in method, OR Declare method can throw exception, force callingfunction to catch or declare exception in turn Example^ void A( ) throws ExceptionType { … }
Designing & Using Exceptions
Not for common cases
checking end of loop
High overhead to perform catch
Overview
Definition & uses
Serializability
Ability to convert a graph of Java objects into astream of data, then convert it back (deserialize)
Marks class as Serializable Supported by Java core libraries Special handling (if needed) using
private void writeObject(java.io.ObjectOutputStream out)^ throws IOException private void readObject(java.io.ObjectInputStream in)^ throws IOException, ClassNotFoundException
Makes a deep copy
Serializability – Deep Copy
// serialize object ByteArrayOutputStream mOut = new ByteArrayOutputStream( ); ObjectOutputStream serializer = new ObjectOutputStream(mOut); serializer.writeObject(serializableObject); serializer.flush( ); // deserialize object ByteArrayInputStream mIn = new ByteArrayInputStream(mOut.
toByteArray( )); ObjectInputStream deserializer = new ObjectInputStream(mIn); Object deepCopyOfOriginalObject = deserializer.readObject( );
Java Serializable Comparison
Y = X.clone( )X
OS.writeObject(x)Y = readObject(IS)