Java Exception Handling and Serialization: A Deep Dive into Effective Java - Prof. Nelson , Study notes of Computer Science

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

Pre 2010

Uploaded on 02/13/2009

koofers-user-clu-1
koofers-user-clu-1 🇺🇸

1

(1)

10 documents

1 / 36

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMSC 132:
Object-Oriented Programming II
1
Exceptions, Serialization,
Effective Java
Department of Computer Science
University of Maryland, College Park
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24

Partial preview of the text

Download Java Exception Handling and Serialization: A Deep Dive into Effective Java - Prof. Nelson and more Study notes Computer Science in PDF only on Docsity!

CMSC 132:

Object-Oriented Programming II

Exceptions, Serialization,

Effective Java

Department of Computer Science

University of Maryland, College Park

Overview

Exceptions

Motivation Representation in Java

Serializability Effective Java

Problem

May not be able to handle error locally

Not enough information in method / class Need more information to decide action

Handle exception in calling function(s) instead

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

Will need to propagate exception to caller(s)

Exception Handling – Exit Program Approach

Exit program with error message / error code

Example^ if (error) {

System.err.println(“Error found”);

// message

System.exit(1);

// error code

} Problem

Drastic solution Event must be handled by user invoking program Program may be able to deal with some exceptions

Exception Handling – Throw Exception

Approach

Throw exception (caught in parent’s catch block)

ExampleA( ) {

if (error) throw new ExceptionType(); } B( ) {

try {

A( );

} catch (ExceptionType e) { ...action... } }

Java exception backtracks tocaller(s) until matching catchblock found

Exception Handling – Throw Exception

Advantages

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

Java Exception class hierarchy

Two types of exceptions

checked & unchecked

Representing Exceptions

Java Exception class hierarchy

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

Class Exception (except RunTimeException) Errors typical program should handle Used for operations prone to error Example

IOException, ClassNotFoundException

Compiler requires “catch or declare”

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

Use exceptions only for rare events

Not for common cases

checking end of loop

High overhead to perform catch

Place statements that jointly accomplish taskinto single try / catch block Use existing Java Exceptions if possible

Overview

Exceptions Serializability

Definition & uses

Effective Java

Serializability

Definition

Ability to convert a graph of Java objects into astream of data, then convert it back (deserialize)

Java.io.Serializable interface

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

Example (X.field = Z)

Y = X

X

Y

Y = X.clone( )X

Y

Z

Z

X, Y

X

Y

OS.writeObject(x)Y = readObject(IS)

X

Y

X^ Z

Y^ Z’