























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
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(
// 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();
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
Object^ Object
Error^ Error
Throwable^ Throwable
Exception^ Exception
ClassNotFoundException^ ClassNotFoundExceptionCloneNotSupportedException^ CloneNotSupportedExceptionIOException^ IOExceptionAWTException^ AWTExceptionRuntimeException^ RuntimeException … LinkageError^ LinkageErrorVirtualMachoneError^ VirtualMachoneErrorAWTError^ AWTError …
ArithmeticException^ ArithmeticExceptionNullPointerException^ NullPointerExceptionIndexOutOfBoundsException^ IndexOutOfBoundsException
Checked^ CheckedUnchecked^ Unchecked NoSuchElementException^ NoSuchElementException …
Representing Exceptions
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
Puzzlers Principles
Effective Java
Effective JavaProgrammingLanguage Guide
Joshua Bloch
Useful tips forJava programming
What's In A Name?
public class Name {
private String myName;public Name(String n) { myName = n; }public boolean equals(Object o) {
if (!(o instanceof Name)) return false;Name n = (Name)o;return myName.equals(n.myName); } public static void main(String[ ] args) {
Set s = new HashSet();s.add(new Name("Donald"));System.out.println(s.contains(new Name("Donald"))); } }
True
False
It Varies Name classviolates JavahashCode( )contract.If you overrideequals( ), mustalso overridehashCode( )!
You're Such A Character
public class Trivial {
public static void main(String args[ ]) {
System.out.print("H" + "a");System.out.print('H' + 'a'); } }
Ha
HaHa
Neither
Prints Ha169'H' + 'a' evaluated as
int
then converted to String!Use string concatenation(+) with care. At least oneoperand must be a String