Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf ·...

163
ECM1406: Data Structures and Team Project Inheritance and Exception Handling Inheritance and Exception Handling 1/16

Transcript of Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf ·...

Page 1: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

ECM1406: Data Structures and Team Project

Inheritance and Exception Handling

Inheritance and Exception Handling 1/16

Page 2: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Inheritance

Represents a “is-a” relationship.

Single inheritanceSubclass derived from one existing class (superclass)

Multiple inheritanceSubclass derived from more than one superclass

Not supported by Javaa class can only extend the definition of one class.

Inheritance Inheritance and Exception Handling 2/16

Page 3: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Inheritance

Represents a “is-a” relationship.

Single inheritanceSubclass derived from one existing class (superclass)

Multiple inheritanceSubclass derived from more than one superclass

Not supported by Javaa class can only extend the definition of one class.

Inheritance Inheritance and Exception Handling 2/16

Page 4: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Inheritance

Represents a “is-a” relationship.

Single inheritanceSubclass derived from one existing class (superclass)

Multiple inheritanceSubclass derived from more than one superclass

Not supported by Javaa class can only extend the definition of one class.

Inheritance Inheritance and Exception Handling 2/16

Page 5: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Inheritance

Represents a “is-a” relationship.

Single inheritanceSubclass derived from one existing class (superclass)

Multiple inheritanceSubclass derived from more than one superclass

Not supported by Javaa class can only extend the definition of one class.

Inheritance Inheritance and Exception Handling 2/16

Page 6: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Inheritance

Represents a “is-a” relationship.

Single inheritanceSubclass derived from one existing class (superclass)

Multiple inheritanceSubclass derived from more than one superclass

Not supported by Javaa class can only extend the definition of one class.

Inheritance Inheritance and Exception Handling 2/16

Page 7: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Inheritance

Represents a “is-a” relationship.

Single inheritanceSubclass derived from one existing class (superclass)

Multiple inheritanceSubclass derived from more than one superclass

Not supported by Javaa class can only extend the definition of one class.

Inheritance Inheritance and Exception Handling 2/16

Page 8: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Inheritance

Represents a “is-a” relationship.

Single inheritanceSubclass derived from one existing class (superclass)

Multiple inheritanceSubclass derived from more than one superclass

Not supported by Javaa class can only extend the definition of one class.

Inheritance Inheritance and Exception Handling 2/16

Page 9: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Inheritance

〈modifer(s)〉 class SubClass extends SuperClass 〈modifier(s)〉 {

classMembers}

Inheritance Inheritance and Exception Handling 3/16

Page 10: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Inheritance

〈modifer(s)〉 class SubClass extends SuperClass 〈modifier(s)〉 {

classMembers}

Inheritance Inheritance and Exception Handling 3/16

Page 11: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Inheritance

〈modifer(s)〉 class SubClass extends SuperClass 〈modifier(s)〉 {

classMembers}

Inheritance Inheritance and Exception Handling 3/16

Page 12: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Inheritance

〈modifer(s)〉 class SubClass extends SuperClass 〈modifier(s)〉 {

classMembers}

Inheritance Inheritance and Exception Handling 3/16

Page 13: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Inheritance

Access to superclass’ membersprivate: a subclass can not access a superclass’ private members.

protected: it can however, access a superclass’ protected members.

Overriding

A subclass can override a method in the superclass.The redefinition only applies to objects of the subclass.The subclass can still access the superclass’s method usingthe super class specifier:

super.method(. . .)

Constructor

The subclass’ constructor can call the superclass’constructor using:

super(. . .)

Must be the first statement in the subclasses constructor.

Inheritance Inheritance and Exception Handling 4/16

Page 14: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Inheritance

Access to superclass’ membersprivate: a subclass can not access a superclass’ private members.

protected: it can however, access a superclass’ protected members.

Overriding

A subclass can override a method in the superclass.The redefinition only applies to objects of the subclass.The subclass can still access the superclass’s method usingthe super class specifier:

super.method(. . .)

Constructor

The subclass’ constructor can call the superclass’constructor using:

super(. . .)

Must be the first statement in the subclasses constructor.

Inheritance Inheritance and Exception Handling 4/16

Page 15: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Inheritance

Access to superclass’ membersprivate: a subclass can not access a superclass’ private members.

protected: it can however, access a superclass’ protected members.

Overriding

A subclass can override a method in the superclass.The redefinition only applies to objects of the subclass.The subclass can still access the superclass’s method usingthe super class specifier:

super.method(. . .)

Constructor

The subclass’ constructor can call the superclass’constructor using:

super(. . .)

Must be the first statement in the subclasses constructor.

Inheritance Inheritance and Exception Handling 4/16

Page 16: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Inheritance

Access to superclass’ membersprivate: a subclass can not access a superclass’ private members.

protected: it can however, access a superclass’ protected members.

Overriding

A subclass can override a method in the superclass.The redefinition only applies to objects of the subclass.The subclass can still access the superclass’s method usingthe super class specifier:

super.method(. . .)

Constructor

The subclass’ constructor can call the superclass’constructor using:

super(. . .)

Must be the first statement in the subclasses constructor.

Inheritance Inheritance and Exception Handling 4/16

Page 17: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Inheritance

Access to superclass’ membersprivate: a subclass can not access a superclass’ private members.

protected: it can however, access a superclass’ protected members.

Overriding

A subclass can override a method in the superclass.The redefinition only applies to objects of the subclass.The subclass can still access the superclass’s method usingthe super class specifier:

super.method(. . .)

Constructor

The subclass’ constructor can call the superclass’constructor using:

super(. . .)

Must be the first statement in the subclasses constructor.

Inheritance Inheritance and Exception Handling 4/16

Page 18: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Inheritance

Access to superclass’ membersprivate: a subclass can not access a superclass’ private members.

protected: it can however, access a superclass’ protected members.

Overriding

A subclass can override a method in the superclass.The redefinition only applies to objects of the subclass.The subclass can still access the superclass’s method usingthe super class specifier:

super.method(. . .)

Constructor

The subclass’ constructor can call the superclass’constructor using:

super(. . .)

Must be the first statement in the subclasses constructor.

Inheritance Inheritance and Exception Handling 4/16

Page 19: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Inheritance

Access to superclass’ membersprivate: a subclass can not access a superclass’ private members.

protected: it can however, access a superclass’ protected members.

Overriding

A subclass can override a method in the superclass.The redefinition only applies to objects of the subclass.The subclass can still access the superclass’s method usingthe super class specifier:

super.method(. . .)

Constructor

The subclass’ constructor can call the superclass’constructor using:

super(. . .)

Must be the first statement in the subclasses constructor.

Inheritance Inheritance and Exception Handling 4/16

Page 20: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Inheritance

Access to superclass’ membersprivate: a subclass can not access a superclass’ private members.

protected: it can however, access a superclass’ protected members.

Overriding

A subclass can override a method in the superclass.The redefinition only applies to objects of the subclass.The subclass can still access the superclass’s method usingthe super class specifier:

super.method(. . .)

Constructor

The subclass’ constructor can call the superclass’constructor using:

super(. . .)

Must be the first statement in the subclasses constructor.

Inheritance Inheritance and Exception Handling 4/16

Page 21: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Inheritance

Access to superclass’ membersprivate: a subclass can not access a superclass’ private members.

protected: it can however, access a superclass’ protected members.

Overriding

A subclass can override a method in the superclass.The redefinition only applies to objects of the subclass.The subclass can still access the superclass’s method usingthe super class specifier: super.method(. . .)

Constructor

The subclass’ constructor can call the superclass’constructor using:

super(. . .)

Must be the first statement in the subclasses constructor.

Inheritance Inheritance and Exception Handling 4/16

Page 22: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Inheritance

Access to superclass’ membersprivate: a subclass can not access a superclass’ private members.

protected: it can however, access a superclass’ protected members.

Overriding

A subclass can override a method in the superclass.The redefinition only applies to objects of the subclass.The subclass can still access the superclass’s method usingthe super class specifier: super.method(. . .)

Constructor

The subclass’ constructor can call the superclass’constructor using:

super(. . .)

Must be the first statement in the subclasses constructor.

Inheritance Inheritance and Exception Handling 4/16

Page 23: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Inheritance

Access to superclass’ membersprivate: a subclass can not access a superclass’ private members.

protected: it can however, access a superclass’ protected members.

Overriding

A subclass can override a method in the superclass.The redefinition only applies to objects of the subclass.The subclass can still access the superclass’s method usingthe super class specifier: super.method(. . .)

Constructor

The subclass’ constructor can call the superclass’constructor using:

super(. . .)

Must be the first statement in the subclasses constructor.

Inheritance Inheritance and Exception Handling 4/16

Page 24: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Inheritance

Access to superclass’ membersprivate: a subclass can not access a superclass’ private members.

protected: it can however, access a superclass’ protected members.

Overriding

A subclass can override a method in the superclass.The redefinition only applies to objects of the subclass.The subclass can still access the superclass’s method usingthe super class specifier: super.method(. . .)

Constructor

The subclass’ constructor can call the superclass’constructor using: super(. . .)

Must be the first statement in the subclasses constructor.

Inheritance Inheritance and Exception Handling 4/16

Page 25: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Inheritance

Access to superclass’ membersprivate: a subclass can not access a superclass’ private members.

protected: it can however, access a superclass’ protected members.

Overriding

A subclass can override a method in the superclass.The redefinition only applies to objects of the subclass.The subclass can still access the superclass’s method usingthe super class specifier: super.method(. . .)

Constructor

The subclass’ constructor can call the superclass’constructor using: super(. . .)

Must be the first statement in the subclasses constructor.

Inheritance Inheritance and Exception Handling 4/16

Page 26: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

The Object Class

All classes inherit from the Object superclass.Public member can be overridden/invoked by an object ofany class.

public Object()

Constructorpublic String toString()

Method to return a string to describe the objectpublic boolean equals(Object obj)

True when current object and obj are the same instance.protected Object clone()

Returns a reference to a copy of the Object.protected void finalize()

Invoked when the object goes out of scope.

The Object Class Inheritance and Exception Handling 5/16

Page 27: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

The Object Class

All classes inherit from the Object superclass.Public member can be overridden/invoked by an object ofany class.

public Object()

Constructorpublic String toString()

Method to return a string to describe the objectpublic boolean equals(Object obj)

True when current object and obj are the same instance.protected Object clone()

Returns a reference to a copy of the Object.protected void finalize()

Invoked when the object goes out of scope.

The Object Class Inheritance and Exception Handling 5/16

Page 28: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

The Object Class

All classes inherit from the Object superclass.Public member can be overridden/invoked by an object ofany class.

public Object()

Constructorpublic String toString()

Method to return a string to describe the objectpublic boolean equals(Object obj)

True when current object and obj are the same instance.protected Object clone()

Returns a reference to a copy of the Object.protected void finalize()

Invoked when the object goes out of scope.

The Object Class Inheritance and Exception Handling 5/16

Page 29: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

The Object Class

All classes inherit from the Object superclass.Public member can be overridden/invoked by an object ofany class.

public Object()

Constructorpublic String toString()

Method to return a string to describe the objectpublic boolean equals(Object obj)

True when current object and obj are the same instance.protected Object clone()

Returns a reference to a copy of the Object.protected void finalize()

Invoked when the object goes out of scope.

The Object Class Inheritance and Exception Handling 5/16

Page 30: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

The Object Class

All classes inherit from the Object superclass.Public member can be overridden/invoked by an object ofany class.

public Object()

Constructorpublic String toString()

Method to return a string to describe the objectpublic boolean equals(Object obj)

True when current object and obj are the same instance.protected Object clone()

Returns a reference to a copy of the Object.protected void finalize()

Invoked when the object goes out of scope.

The Object Class Inheritance and Exception Handling 5/16

Page 31: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

The Object Class

All classes inherit from the Object superclass.Public member can be overridden/invoked by an object ofany class.

public Object()

Constructorpublic String toString()

Method to return a string to describe the objectpublic boolean equals(Object obj)

True when current object and obj are the same instance.protected Object clone()

Returns a reference to a copy of the Object.protected void finalize()

Invoked when the object goes out of scope.

The Object Class Inheritance and Exception Handling 5/16

Page 32: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

The Object Class

All classes inherit from the Object superclass.Public member can be overridden/invoked by an object ofany class.

public Object()

Constructorpublic String toString()

Method to return a string to describe the objectpublic boolean equals(Object obj)

True when current object and obj are the same instance.protected Object clone()

Returns a reference to a copy of the Object.protected void finalize()

Invoked when the object goes out of scope.

The Object Class Inheritance and Exception Handling 5/16

Page 33: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

The Object Class

All classes inherit from the Object superclass.Public member can be overridden/invoked by an object ofany class.

public Object()

Constructorpublic String toString()

Method to return a string to describe the objectpublic boolean equals(Object obj)

True when current object and obj are the same instance.protected Object clone()

Returns a reference to a copy of the Object.protected void finalize()

Invoked when the object goes out of scope.

The Object Class Inheritance and Exception Handling 5/16

Page 34: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

The Object Class

All classes inherit from the Object superclass.Public member can be overridden/invoked by an object ofany class.

public Object()

Constructorpublic String toString()

Method to return a string to describe the objectpublic boolean equals(Object obj)

True when current object and obj are the same instance.protected Object clone()

Returns a reference to a copy of the Object.protected void finalize()

Invoked when the object goes out of scope.

The Object Class Inheritance and Exception Handling 5/16

Page 35: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

The Object Class

All classes inherit from the Object superclass.Public member can be overridden/invoked by an object ofany class.

public Object()

Constructorpublic String toString()

Method to return a string to describe the objectpublic boolean equals(Object obj)

True when current object and obj are the same instance.protected Object clone()

Returns a reference to a copy of the Object.protected void finalize()

Invoked when the object goes out of scope.

The Object Class Inheritance and Exception Handling 5/16

Page 36: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

The Object Class

All classes inherit from the Object superclass.Public member can be overridden/invoked by an object ofany class.

public Object()

Constructorpublic String toString()

Method to return a string to describe the objectpublic boolean equals(Object obj)

True when current object and obj are the same instance.protected Object clone()

Returns a reference to a copy of the Object.protected void finalize()

Invoked when the object goes out of scope.

The Object Class Inheritance and Exception Handling 5/16

Page 37: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

The Object Class

All classes inherit from the Object superclass.Public member can be overridden/invoked by an object ofany class.

public Object()

Constructorpublic String toString()

Method to return a string to describe the objectpublic boolean equals(Object obj)

True when current object and obj are the same instance.protected Object clone()

Returns a reference to a copy of the Object.protected void finalize()

Invoked when the object goes out of scope.

The Object Class Inheritance and Exception Handling 5/16

Page 38: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

The Object Class

All classes inherit from the Object superclass.Public member can be overridden/invoked by an object ofany class.

public Object()

Constructorpublic String toString()

Method to return a string to describe the objectpublic boolean equals(Object obj)

True when current object and obj are the same instance.protected Object clone()

Returns a reference to a copy of the Object.protected void finalize()

Invoked when the object goes out of scope.

The Object Class Inheritance and Exception Handling 5/16

Page 39: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Objects of Superclasses and Subclasses

Automatic CastingReference to a subclass can not refer to a superclass object.Reference to a superclass can refer to its subclass objects.

Class Cast ExceptionOccurs if the class cast is not allowed.Casting up the inheritance tree is not allowed.Casting down the tree is.

Dynamic BindingMethod executed determined at execution time, notcompile time.

instanceof

Determines whether reference variable that points toobject is of particular class type.if ( object instanceof Class ) . . .

The Object Class Inheritance and Exception Handling 6/16

Page 40: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Objects of Superclasses and Subclasses

Automatic CastingReference to a subclass can not refer to a superclass object.Reference to a superclass can refer to its subclass objects.

Class Cast ExceptionOccurs if the class cast is not allowed.Casting up the inheritance tree is not allowed.Casting down the tree is.

Dynamic BindingMethod executed determined at execution time, notcompile time.

instanceof

Determines whether reference variable that points toobject is of particular class type.if ( object instanceof Class ) . . .

The Object Class Inheritance and Exception Handling 6/16

Page 41: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Objects of Superclasses and Subclasses

Automatic CastingReference to a subclass can not refer to a superclass object.Reference to a superclass can refer to its subclass objects.

Class Cast ExceptionOccurs if the class cast is not allowed.Casting up the inheritance tree is not allowed.Casting down the tree is.

Dynamic BindingMethod executed determined at execution time, notcompile time.

instanceof

Determines whether reference variable that points toobject is of particular class type.if ( object instanceof Class ) . . .

The Object Class Inheritance and Exception Handling 6/16

Page 42: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Objects of Superclasses and Subclasses

Automatic CastingReference to a subclass can not refer to a superclass object.Reference to a superclass can refer to its subclass objects.

Class Cast ExceptionOccurs if the class cast is not allowed.Casting up the inheritance tree is not allowed.Casting down the tree is.

Dynamic BindingMethod executed determined at execution time, notcompile time.

instanceof

Determines whether reference variable that points toobject is of particular class type.if ( object instanceof Class ) . . .

The Object Class Inheritance and Exception Handling 6/16

Page 43: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Objects of Superclasses and Subclasses

Automatic CastingReference to a subclass can not refer to a superclass object.Reference to a superclass can refer to its subclass objects.

Class Cast ExceptionOccurs if the class cast is not allowed.Casting up the inheritance tree is not allowed.Casting down the tree is.

Dynamic BindingMethod executed determined at execution time, notcompile time.

instanceof

Determines whether reference variable that points toobject is of particular class type.if ( object instanceof Class ) . . .

The Object Class Inheritance and Exception Handling 6/16

Page 44: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Objects of Superclasses and Subclasses

Automatic CastingReference to a subclass can not refer to a superclass object.Reference to a superclass can refer to its subclass objects.

Class Cast ExceptionOccurs if the class cast is not allowed.Casting up the inheritance tree is not allowed.Casting down the tree is.

Dynamic BindingMethod executed determined at execution time, notcompile time.

instanceof

Determines whether reference variable that points toobject is of particular class type.if ( object instanceof Class ) . . .

The Object Class Inheritance and Exception Handling 6/16

Page 45: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Objects of Superclasses and Subclasses

Automatic CastingReference to a subclass can not refer to a superclass object.Reference to a superclass can refer to its subclass objects.

Class Cast ExceptionOccurs if the class cast is not allowed.Casting up the inheritance tree is not allowed.Casting down the tree is.

Dynamic BindingMethod executed determined at execution time, notcompile time.

instanceof

Determines whether reference variable that points toobject is of particular class type.if ( object instanceof Class ) . . .

The Object Class Inheritance and Exception Handling 6/16

Page 46: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Objects of Superclasses and Subclasses

Automatic CastingReference to a subclass can not refer to a superclass object.Reference to a superclass can refer to its subclass objects.

Class Cast ExceptionOccurs if the class cast is not allowed.Casting up the inheritance tree is not allowed.Casting down the tree is.

Dynamic BindingMethod executed determined at execution time, notcompile time.

instanceof

Determines whether reference variable that points toobject is of particular class type.if ( object instanceof Class ) . . .

The Object Class Inheritance and Exception Handling 6/16

Page 47: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Objects of Superclasses and Subclasses

Automatic CastingReference to a subclass can not refer to a superclass object.Reference to a superclass can refer to its subclass objects.

Class Cast ExceptionOccurs if the class cast is not allowed.Casting up the inheritance tree is not allowed.Casting down the tree is.

Dynamic BindingMethod executed determined at execution time, notcompile time.

instanceof

Determines whether reference variable that points toobject is of particular class type.if ( object instanceof Class ) . . .

The Object Class Inheritance and Exception Handling 6/16

Page 48: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Objects of Superclasses and Subclasses

Automatic CastingReference to a subclass can not refer to a superclass object.Reference to a superclass can refer to its subclass objects.

Class Cast ExceptionOccurs if the class cast is not allowed.Casting up the inheritance tree is not allowed.Casting down the tree is.

Dynamic BindingMethod executed determined at execution time, notcompile time.

instanceof

Determines whether reference variable that points toobject is of particular class type.if ( object instanceof Class ) . . .

The Object Class Inheritance and Exception Handling 6/16

Page 49: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Objects of Superclasses and Subclasses

Automatic CastingReference to a subclass can not refer to a superclass object.Reference to a superclass can refer to its subclass objects.

Class Cast ExceptionOccurs if the class cast is not allowed.Casting up the inheritance tree is not allowed.Casting down the tree is.

Dynamic BindingMethod executed determined at execution time, notcompile time.

instanceof

Determines whether reference variable that points toobject is of particular class type.if ( object instanceof Class ) . . .

The Object Class Inheritance and Exception Handling 6/16

Page 50: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Objects of Superclasses and Subclasses

Automatic CastingReference to a subclass can not refer to a superclass object.Reference to a superclass can refer to its subclass objects.

Class Cast ExceptionOccurs if the class cast is not allowed.Casting up the inheritance tree is not allowed.Casting down the tree is.

Dynamic BindingMethod executed determined at execution time, notcompile time.

instanceof

Determines whether reference variable that points toobject is of particular class type.if ( object instanceof Class ) . . .

The Object Class Inheritance and Exception Handling 6/16

Page 51: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Objects of Superclasses and Subclasses

Automatic CastingReference to a subclass can not refer to a superclass object.Reference to a superclass can refer to its subclass objects.

Class Cast ExceptionOccurs if the class cast is not allowed.Casting up the inheritance tree is not allowed.Casting down the tree is.

Dynamic BindingMethod executed determined at execution time, notcompile time.

instanceof

Determines whether reference variable that points toobject is of particular class type.if ( object instanceof Class ) . . .

The Object Class Inheritance and Exception Handling 6/16

Page 52: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Abstract Methods and Classes

Abstract Method

A method that has only the heading with no body.Must be declared abstract.A subclass must defined the method.

Abstract Class

Class that is declared using the abstract modifier.Cannot have an instance of abstract class type.Can refer to an abstract class (a reference variable).Can instantiate object of subclass of abstract class.Subclass must give definition of all abstract methods.

Composition

Class members are objects of another class type.Represents a “has-a” relationship between classes.

Abstraction Inheritance and Exception Handling 7/16

Page 53: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Abstract Methods and Classes

Abstract Method

A method that has only the heading with no body.Must be declared abstract.A subclass must defined the method.

Abstract Class

Class that is declared using the abstract modifier.Cannot have an instance of abstract class type.Can refer to an abstract class (a reference variable).Can instantiate object of subclass of abstract class.Subclass must give definition of all abstract methods.

Composition

Class members are objects of another class type.Represents a “has-a” relationship between classes.

Abstraction Inheritance and Exception Handling 7/16

Page 54: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Abstract Methods and Classes

Abstract Method

A method that has only the heading with no body.Must be declared abstract.A subclass must defined the method.

Abstract Class

Class that is declared using the abstract modifier.Cannot have an instance of abstract class type.Can refer to an abstract class (a reference variable).Can instantiate object of subclass of abstract class.Subclass must give definition of all abstract methods.

Composition

Class members are objects of another class type.Represents a “has-a” relationship between classes.

Abstraction Inheritance and Exception Handling 7/16

Page 55: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Abstract Methods and Classes

Abstract Method

A method that has only the heading with no body.Must be declared abstract.A subclass must defined the method.

Abstract Class

Class that is declared using the abstract modifier.Cannot have an instance of abstract class type.Can refer to an abstract class (a reference variable).Can instantiate object of subclass of abstract class.Subclass must give definition of all abstract methods.

Composition

Class members are objects of another class type.Represents a “has-a” relationship between classes.

Abstraction Inheritance and Exception Handling 7/16

Page 56: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Abstract Methods and Classes

Abstract Method

A method that has only the heading with no body.Must be declared abstract.A subclass must defined the method.

Abstract Class

Class that is declared using the abstract modifier.Cannot have an instance of abstract class type.Can refer to an abstract class (a reference variable).Can instantiate object of subclass of abstract class.Subclass must give definition of all abstract methods.

Composition

Class members are objects of another class type.Represents a “has-a” relationship between classes.

Abstraction Inheritance and Exception Handling 7/16

Page 57: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Abstract Methods and Classes

Abstract Method

A method that has only the heading with no body.Must be declared abstract.A subclass must defined the method.

Abstract Class

Class that is declared using the abstract modifier.Cannot have an instance of abstract class type.Can refer to an abstract class (a reference variable).Can instantiate object of subclass of abstract class.Subclass must give definition of all abstract methods.

Composition

Class members are objects of another class type.Represents a “has-a” relationship between classes.

Abstraction Inheritance and Exception Handling 7/16

Page 58: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Abstract Methods and Classes

Abstract Method

A method that has only the heading with no body.Must be declared abstract.A subclass must defined the method.

Abstract Class

Class that is declared using the abstract modifier.Cannot have an instance of abstract class type.Can refer to an abstract class (a reference variable).Can instantiate object of subclass of abstract class.Subclass must give definition of all abstract methods.

Composition

Class members are objects of another class type.Represents a “has-a” relationship between classes.

Abstraction Inheritance and Exception Handling 7/16

Page 59: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Abstract Methods and Classes

Abstract Method

A method that has only the heading with no body.Must be declared abstract.A subclass must defined the method.

Abstract Class

Class that is declared using the abstract modifier.Cannot have an instance of abstract class type.Can refer to an abstract class (a reference variable).Can instantiate object of subclass of abstract class.Subclass must give definition of all abstract methods.

Composition

Class members are objects of another class type.Represents a “has-a” relationship between classes.

Abstraction Inheritance and Exception Handling 7/16

Page 60: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Abstract Methods and Classes

Abstract Method

A method that has only the heading with no body.Must be declared abstract.A subclass must defined the method.

Abstract Class

Class that is declared using the abstract modifier.Cannot have an instance of abstract class type.Can refer to an abstract class (a reference variable).Can instantiate object of subclass of abstract class.Subclass must give definition of all abstract methods.

Composition

Class members are objects of another class type.Represents a “has-a” relationship between classes.

Abstraction Inheritance and Exception Handling 7/16

Page 61: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Abstract Methods and Classes

Abstract Method

A method that has only the heading with no body.Must be declared abstract.A subclass must defined the method.

Abstract Class

Class that is declared using the abstract modifier.Cannot have an instance of abstract class type.Can refer to an abstract class (a reference variable).Can instantiate object of subclass of abstract class.Subclass must give definition of all abstract methods.

Composition

Class members are objects of another class type.Represents a “has-a” relationship between classes.

Abstraction Inheritance and Exception Handling 7/16

Page 62: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Abstract Methods and Classes

Abstract Method

A method that has only the heading with no body.Must be declared abstract.A subclass must defined the method.

Abstract Class

Class that is declared using the abstract modifier.Cannot have an instance of abstract class type.Can refer to an abstract class (a reference variable).Can instantiate object of subclass of abstract class.Subclass must give definition of all abstract methods.

Composition

Class members are objects of another class type.Represents a “has-a” relationship between classes.

Abstraction Inheritance and Exception Handling 7/16

Page 63: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Abstract Methods and Classes

Abstract Method

A method that has only the heading with no body.Must be declared abstract.A subclass must defined the method.

Abstract Class

Class that is declared using the abstract modifier.Cannot have an instance of abstract class type.Can refer to an abstract class (a reference variable).Can instantiate object of subclass of abstract class.Subclass must give definition of all abstract methods.

Composition

Class members are objects of another class type.Represents a “has-a” relationship between classes.

Abstraction Inheritance and Exception Handling 7/16

Page 64: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Abstract Methods and Classes

Abstract Method

A method that has only the heading with no body.Must be declared abstract.A subclass must defined the method.

Abstract Class

Class that is declared using the abstract modifier.Cannot have an instance of abstract class type.Can refer to an abstract class (a reference variable).Can instantiate object of subclass of abstract class.Subclass must give definition of all abstract methods.

Composition

Class members are objects of another class type.Represents a “has-a” relationship between classes.

Abstraction Inheritance and Exception Handling 7/16

Page 65: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Abstract Methods and Classes

Abstract Method

A method that has only the heading with no body.Must be declared abstract.A subclass must defined the method.

Abstract Class

Class that is declared using the abstract modifier.Cannot have an instance of abstract class type.Can refer to an abstract class (a reference variable).Can instantiate object of subclass of abstract class.Subclass must give definition of all abstract methods.

Composition

Class members are objects of another class type.Represents a “has-a” relationship between classes.

Abstraction Inheritance and Exception Handling 7/16

Page 66: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Exceptions

An occurrence of an undesirable situation that can be detectedduring program execution.

ExamplesDivision by zero.An array index that goes out of bounds.Trying to open an input file that does not exist.

Exceptions Inheritance and Exception Handling 8/16

Page 67: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Exceptions

An occurrence of an undesirable situation that can be detectedduring program execution.

ExamplesDivision by zero.An array index that goes out of bounds.Trying to open an input file that does not exist.

Exceptions Inheritance and Exception Handling 8/16

Page 68: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Exceptions

An occurrence of an undesirable situation that can be detectedduring program execution.

ExamplesDivision by zero.An array index that goes out of bounds.Trying to open an input file that does not exist.

Exceptions Inheritance and Exception Handling 8/16

Page 69: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Exceptions

An occurrence of an undesirable situation that can be detectedduring program execution.

ExamplesDivision by zero.An array index that goes out of bounds.Trying to open an input file that does not exist.

Exceptions Inheritance and Exception Handling 8/16

Page 70: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Exceptions

An occurrence of an undesirable situation that can be detectedduring program execution.

ExamplesDivision by zero.An array index that goes out of bounds.Trying to open an input file that does not exist.

Exceptions Inheritance and Exception Handling 8/16

Page 71: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Exceptions

An occurrence of an undesirable situation that can be detectedduring program execution.

ExamplesDivision by zero.An array index that goes out of bounds.Trying to open an input file that does not exist.

Exceptions Inheritance and Exception Handling 8/16

Page 72: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Exceptions

An occurrence of an undesirable situation that can be detectedduring program execution.

ExamplesDivision by zero.An array index that goes out of bounds.Trying to open an input file that does not exist.

Exceptions Inheritance and Exception Handling 8/16

Page 73: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

The Throwable Class

public Throwable()

Default constructor: no message.public Throwable(String msg)

Constructor: set message to msg.public String getMessage()

Return the objects message.public void printStackTrace()

Print a stack trace showing the sequence of method callswhen the exception occurred.public void printStackTrace(PrintStream stream)

public void printStackTrace(PrintWriter stream)

As printStackTrace() but sends output to stream.public String toString()

Returns a string representing the Throwable object.

Exceptions Inheritance and Exception Handling 9/16

Page 74: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

The Throwable Class

public Throwable()

Default constructor: no message.public Throwable(String msg)

Constructor: set message to msg.public String getMessage()

Return the objects message.public void printStackTrace()

Print a stack trace showing the sequence of method callswhen the exception occurred.public void printStackTrace(PrintStream stream)

public void printStackTrace(PrintWriter stream)

As printStackTrace() but sends output to stream.public String toString()

Returns a string representing the Throwable object.

Exceptions Inheritance and Exception Handling 9/16

Page 75: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

The Throwable Class

public Throwable()

Default constructor: no message.public Throwable(String msg)

Constructor: set message to msg.public String getMessage()

Return the objects message.public void printStackTrace()

Print a stack trace showing the sequence of method callswhen the exception occurred.public void printStackTrace(PrintStream stream)

public void printStackTrace(PrintWriter stream)

As printStackTrace() but sends output to stream.public String toString()

Returns a string representing the Throwable object.

Exceptions Inheritance and Exception Handling 9/16

Page 76: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

The Throwable Class

public Throwable()

Default constructor: no message.public Throwable(String msg)

Constructor: set message to msg.public String getMessage()

Return the objects message.public void printStackTrace()

Print a stack trace showing the sequence of method callswhen the exception occurred.public void printStackTrace(PrintStream stream)

public void printStackTrace(PrintWriter stream)

As printStackTrace() but sends output to stream.public String toString()

Returns a string representing the Throwable object.

Exceptions Inheritance and Exception Handling 9/16

Page 77: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

The Throwable Class

public Throwable()

Default constructor: no message.public Throwable(String msg)

Constructor: set message to msg.public String getMessage()

Return the objects message.public void printStackTrace()

Print a stack trace showing the sequence of method callswhen the exception occurred.public void printStackTrace(PrintStream stream)

public void printStackTrace(PrintWriter stream)

As printStackTrace() but sends output to stream.public String toString()

Returns a string representing the Throwable object.

Exceptions Inheritance and Exception Handling 9/16

Page 78: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

The Throwable Class

public Throwable()

Default constructor: no message.public Throwable(String msg)

Constructor: set message to msg.public String getMessage()

Return the objects message.public void printStackTrace()

Print a stack trace showing the sequence of method callswhen the exception occurred.public void printStackTrace(PrintStream stream)

public void printStackTrace(PrintWriter stream)

As printStackTrace() but sends output to stream.public String toString()

Returns a string representing the Throwable object.

Exceptions Inheritance and Exception Handling 9/16

Page 79: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

The Throwable Class

public Throwable()

Default constructor: no message.public Throwable(String msg)

Constructor: set message to msg.public String getMessage()

Return the objects message.public void printStackTrace()

Print a stack trace showing the sequence of method callswhen the exception occurred.public void printStackTrace(PrintStream stream)

public void printStackTrace(PrintWriter stream)

As printStackTrace() but sends output to stream.public String toString()

Returns a string representing the Throwable object.

Exceptions Inheritance and Exception Handling 9/16

Page 80: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

The Exception Class

Subclass of class ThrowableSuperclass of classes designed to handle exceptions.

Various types of exceptionsI/O exceptionsNumber format exceptionsArray index out of bounds exceptionFile not found exceptions

Exception CategoriesThe different exception categories are separate classes andcontained in various packages.

The Exception Classpublic Exception()

public Exception(String msg)

Constructors inherited from Throwable.Exceptions Inheritance and Exception Handling 10/16

Page 81: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

The Exception Class

Subclass of class ThrowableSuperclass of classes designed to handle exceptions.

Various types of exceptionsI/O exceptionsNumber format exceptionsArray index out of bounds exceptionFile not found exceptions

Exception CategoriesThe different exception categories are separate classes andcontained in various packages.

The Exception Classpublic Exception()

public Exception(String msg)

Constructors inherited from Throwable.Exceptions Inheritance and Exception Handling 10/16

Page 82: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

The Exception Class

Subclass of class ThrowableSuperclass of classes designed to handle exceptions.

Various types of exceptionsI/O exceptionsNumber format exceptionsArray index out of bounds exceptionFile not found exceptions

Exception CategoriesThe different exception categories are separate classes andcontained in various packages.

The Exception Classpublic Exception()

public Exception(String msg)

Constructors inherited from Throwable.Exceptions Inheritance and Exception Handling 10/16

Page 83: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

The Exception Class

Subclass of class ThrowableSuperclass of classes designed to handle exceptions.

Various types of exceptionsI/O exceptionsNumber format exceptionsArray index out of bounds exceptionFile not found exceptions

Exception CategoriesThe different exception categories are separate classes andcontained in various packages.

The Exception Classpublic Exception()

public Exception(String msg)

Constructors inherited from Throwable.Exceptions Inheritance and Exception Handling 10/16

Page 84: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

The Exception Class

Subclass of class ThrowableSuperclass of classes designed to handle exceptions.

Various types of exceptionsI/O exceptionsNumber format exceptionsArray index out of bounds exceptionFile not found exceptions

Exception CategoriesThe different exception categories are separate classes andcontained in various packages.

The Exception Classpublic Exception()

public Exception(String msg)

Constructors inherited from Throwable.Exceptions Inheritance and Exception Handling 10/16

Page 85: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

The Exception Class

Subclass of class ThrowableSuperclass of classes designed to handle exceptions.

Various types of exceptionsI/O exceptionsNumber format exceptionsArray index out of bounds exceptionFile not found exceptions

Exception CategoriesThe different exception categories are separate classes andcontained in various packages.

The Exception Classpublic Exception()

public Exception(String msg)

Constructors inherited from Throwable.Exceptions Inheritance and Exception Handling 10/16

Page 86: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

The Exception Class

Subclass of class ThrowableSuperclass of classes designed to handle exceptions.

Various types of exceptionsI/O exceptionsNumber format exceptionsArray index out of bounds exceptionFile not found exceptions

Exception CategoriesThe different exception categories are separate classes andcontained in various packages.

The Exception Classpublic Exception()

public Exception(String msg)

Constructors inherited from Throwable.Exceptions Inheritance and Exception Handling 10/16

Page 87: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

The Exception Class

Subclass of class ThrowableSuperclass of classes designed to handle exceptions.

Various types of exceptionsI/O exceptionsNumber format exceptionsArray index out of bounds exceptionFile not found exceptions

Exception CategoriesThe different exception categories are separate classes andcontained in various packages.

The Exception Classpublic Exception()

public Exception(String msg)

Constructors inherited from Throwable.Exceptions Inheritance and Exception Handling 10/16

Page 88: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

The Exception Class

Subclass of class ThrowableSuperclass of classes designed to handle exceptions.

Various types of exceptionsI/O exceptionsNumber format exceptionsArray index out of bounds exceptionFile not found exceptions

Exception CategoriesThe different exception categories are separate classes andcontained in various packages.

The Exception Classpublic Exception()

public Exception(String msg)

Constructors inherited from Throwable.Exceptions Inheritance and Exception Handling 10/16

Page 89: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

The Exception Class

Subclass of class ThrowableSuperclass of classes designed to handle exceptions.

Various types of exceptionsI/O exceptionsNumber format exceptionsArray index out of bounds exceptionFile not found exceptions

Exception CategoriesThe different exception categories are separate classes andcontained in various packages.

The Exception Classpublic Exception()

public Exception(String msg)

Constructors inherited from Throwable.Exceptions Inheritance and Exception Handling 10/16

Page 90: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

The Exception Class

Subclass of class ThrowableSuperclass of classes designed to handle exceptions.

Various types of exceptionsI/O exceptionsNumber format exceptionsArray index out of bounds exceptionFile not found exceptions

Exception CategoriesThe different exception categories are separate classes andcontained in various packages.

The Exception Classpublic Exception()

public Exception(String msg)

Constructors inherited from Throwable.Exceptions Inheritance and Exception Handling 10/16

Page 91: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

The Exception Class

Subclass of class ThrowableSuperclass of classes designed to handle exceptions.

Various types of exceptionsI/O exceptionsNumber format exceptionsArray index out of bounds exceptionFile not found exceptions

Exception CategoriesThe different exception categories are separate classes andcontained in various packages.

The Exception Classpublic Exception()

public Exception(String msg)

Constructors inherited from Throwable.Exceptions Inheritance and Exception Handling 10/16

Page 92: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

The Exception Class

Subclass of class ThrowableSuperclass of classes designed to handle exceptions.

Various types of exceptionsI/O exceptionsNumber format exceptionsArray index out of bounds exceptionFile not found exceptions

Exception CategoriesThe different exception categories are separate classes andcontained in various packages.

The Exception Classpublic Exception()

public Exception(String msg)

Constructors inherited from Throwable.Exceptions Inheritance and Exception Handling 10/16

Page 93: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

The Exception Class

Subclass of class ThrowableSuperclass of classes designed to handle exceptions.

Various types of exceptionsI/O exceptionsNumber format exceptionsArray index out of bounds exceptionFile not found exceptions

Exception CategoriesThe different exception categories are separate classes andcontained in various packages.

The Exception Classpublic Exception()

public Exception(String msg)

Constructors inherited from Throwable.Exceptions Inheritance and Exception Handling 10/16

Page 94: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Some Exception Classes

ArithmeticException An arithmetic error, such as division by zero.

ArrayIndexOutOfBoundsExceptionArray index < 0 or > array length.

FileNotFoundException Referencing a file that cannot be found.

IllegalArgumentExceptionCalling a method with illegal arguments.

IndexOutOfBoundsExceptionAn array index is out of bounds.

NullPointerException Reference an object which has not beeninstantiated.

NumberFormatException Use of an illegal number format.

StringIndexOutOfBoundsExceptionString index < 0 or > length of string.

Exceptions Inheritance and Exception Handling 11/16

Page 95: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Some Exception Classes

ArithmeticException An arithmetic error, such as division by zero.

ArrayIndexOutOfBoundsExceptionArray index < 0 or > array length.

FileNotFoundException Referencing a file that cannot be found.

IllegalArgumentExceptionCalling a method with illegal arguments.

IndexOutOfBoundsExceptionAn array index is out of bounds.

NullPointerException Reference an object which has not beeninstantiated.

NumberFormatException Use of an illegal number format.

StringIndexOutOfBoundsExceptionString index < 0 or > length of string.

Exceptions Inheritance and Exception Handling 11/16

Page 96: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Some Exception Classes

ArithmeticException An arithmetic error, such as division by zero.

ArrayIndexOutOfBoundsExceptionArray index < 0 or > array length.

FileNotFoundException Referencing a file that cannot be found.

IllegalArgumentExceptionCalling a method with illegal arguments.

IndexOutOfBoundsExceptionAn array index is out of bounds.

NullPointerException Reference an object which has not beeninstantiated.

NumberFormatException Use of an illegal number format.

StringIndexOutOfBoundsExceptionString index < 0 or > length of string.

Exceptions Inheritance and Exception Handling 11/16

Page 97: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Some Exception Classes

ArithmeticException An arithmetic error, such as division by zero.

ArrayIndexOutOfBoundsExceptionArray index < 0 or > array length.

FileNotFoundException Referencing a file that cannot be found.

IllegalArgumentExceptionCalling a method with illegal arguments.

IndexOutOfBoundsExceptionAn array index is out of bounds.

NullPointerException Reference an object which has not beeninstantiated.

NumberFormatException Use of an illegal number format.

StringIndexOutOfBoundsExceptionString index < 0 or > length of string.

Exceptions Inheritance and Exception Handling 11/16

Page 98: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Some Exception Classes

ArithmeticException An arithmetic error, such as division by zero.

ArrayIndexOutOfBoundsExceptionArray index < 0 or > array length.

FileNotFoundException Referencing a file that cannot be found.

IllegalArgumentExceptionCalling a method with illegal arguments.

IndexOutOfBoundsExceptionAn array index is out of bounds.

NullPointerException Reference an object which has not beeninstantiated.

NumberFormatException Use of an illegal number format.

StringIndexOutOfBoundsExceptionString index < 0 or > length of string.

Exceptions Inheritance and Exception Handling 11/16

Page 99: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Some Exception Classes

ArithmeticException An arithmetic error, such as division by zero.

ArrayIndexOutOfBoundsExceptionArray index < 0 or > array length.

FileNotFoundException Referencing a file that cannot be found.

IllegalArgumentExceptionCalling a method with illegal arguments.

IndexOutOfBoundsExceptionAn array index is out of bounds.

NullPointerException Reference an object which has not beeninstantiated.

NumberFormatException Use of an illegal number format.

StringIndexOutOfBoundsExceptionString index < 0 or > length of string.

Exceptions Inheritance and Exception Handling 11/16

Page 100: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Some Exception Classes

ArithmeticException An arithmetic error, such as division by zero.

ArrayIndexOutOfBoundsExceptionArray index < 0 or > array length.

FileNotFoundException Referencing a file that cannot be found.

IllegalArgumentExceptionCalling a method with illegal arguments.

IndexOutOfBoundsExceptionAn array index is out of bounds.

NullPointerException Reference an object which has not beeninstantiated.

NumberFormatException Use of an illegal number format.

StringIndexOutOfBoundsExceptionString index < 0 or > length of string.

Exceptions Inheritance and Exception Handling 11/16

Page 101: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Some Exception Classes

ArithmeticException An arithmetic error, such as division by zero.

ArrayIndexOutOfBoundsExceptionArray index < 0 or > array length.

FileNotFoundException Referencing a file that cannot be found.

IllegalArgumentExceptionCalling a method with illegal arguments.

IndexOutOfBoundsExceptionAn array index is out of bounds.

NullPointerException Reference an object which has not beeninstantiated.

NumberFormatException Use of an illegal number format.

StringIndexOutOfBoundsExceptionString index < 0 or > length of string.

Exceptions Inheritance and Exception Handling 11/16

Page 102: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Some Exception Classes

ArithmeticException An arithmetic error, such as division by zero.

ArrayIndexOutOfBoundsExceptionArray index < 0 or > array length.

FileNotFoundException Referencing a file that cannot be found.

IllegalArgumentExceptionCalling a method with illegal arguments.

IndexOutOfBoundsExceptionAn array index is out of bounds.

NullPointerException Reference an object which has not beeninstantiated.

NumberFormatException Use of an illegal number format.

StringIndexOutOfBoundsExceptionString index < 0 or > length of string.

Exceptions Inheritance and Exception Handling 11/16

Page 103: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Exceptions Thrown by Methods

NumberFormatExceptionparseInt(String str) str does not contain a valid int.parseDouble(String str) str does not contain a valid double.valueOf(String str) str does not contain a valid value.

NullPointerExceptionString(String str) str is null.indexOf(String str) str is null.lastIndexOf(String str) str is null.

StringIndexOutOfBoundsException

charAt(int a) a is not a valid index.substring(int a) a is not a valid index.substring(int a, int b) a and/or b is not a valid index.

Exceptions Inheritance and Exception Handling 12/16

Page 104: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Exceptions Thrown by Methods

NumberFormatExceptionparseInt(String str) str does not contain a valid int.parseDouble(String str) str does not contain a valid double.valueOf(String str) str does not contain a valid value.

NullPointerExceptionString(String str) str is null.indexOf(String str) str is null.lastIndexOf(String str) str is null.

StringIndexOutOfBoundsException

charAt(int a) a is not a valid index.substring(int a) a is not a valid index.substring(int a, int b) a and/or b is not a valid index.

Exceptions Inheritance and Exception Handling 12/16

Page 105: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Exceptions Thrown by Methods

NumberFormatExceptionparseInt(String str) str does not contain a valid int.parseDouble(String str) str does not contain a valid double.valueOf(String str) str does not contain a valid value.

NullPointerExceptionString(String str) str is null.indexOf(String str) str is null.lastIndexOf(String str) str is null.

StringIndexOutOfBoundsException

charAt(int a) a is not a valid index.substring(int a) a is not a valid index.substring(int a, int b) a and/or b is not a valid index.

Exceptions Inheritance and Exception Handling 12/16

Page 106: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Exceptions Thrown by Methods

NumberFormatExceptionparseInt(String str) str does not contain a valid int.parseDouble(String str) str does not contain a valid double.valueOf(String str) str does not contain a valid value.

NullPointerExceptionString(String str) str is null.indexOf(String str) str is null.lastIndexOf(String str) str is null.

StringIndexOutOfBoundsException

charAt(int a) a is not a valid index.substring(int a) a is not a valid index.substring(int a, int b) a and/or b is not a valid index.

Exceptions Inheritance and Exception Handling 12/16

Page 107: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Exceptions Thrown by Methods

NumberFormatExceptionparseInt(String str) str does not contain a valid int.parseDouble(String str) str does not contain a valid double.valueOf(String str) str does not contain a valid value.

NullPointerExceptionString(String str) str is null.indexOf(String str) str is null.lastIndexOf(String str) str is null.

StringIndexOutOfBoundsException

charAt(int a) a is not a valid index.substring(int a) a is not a valid index.substring(int a, int b) a and/or b is not a valid index.

Exceptions Inheritance and Exception Handling 12/16

Page 108: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Exceptions Thrown by Methods

NumberFormatExceptionparseInt(String str) str does not contain a valid int.parseDouble(String str) str does not contain a valid double.valueOf(String str) str does not contain a valid value.

NullPointerExceptionString(String str) str is null.indexOf(String str) str is null.lastIndexOf(String str) str is null.

StringIndexOutOfBoundsException

charAt(int a) a is not a valid index.substring(int a) a is not a valid index.substring(int a, int b) a and/or b is not a valid index.

Exceptions Inheritance and Exception Handling 12/16

Page 109: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Exceptions Thrown by Methods

NumberFormatExceptionparseInt(String str) str does not contain a valid int.parseDouble(String str) str does not contain a valid double.valueOf(String str) str does not contain a valid value.

NullPointerExceptionString(String str) str is null.indexOf(String str) str is null.lastIndexOf(String str) str is null.

StringIndexOutOfBoundsException

charAt(int a) a is not a valid index.substring(int a) a is not a valid index.substring(int a, int b) a and/or b is not a valid index.

Exceptions Inheritance and Exception Handling 12/16

Page 110: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Exceptions Thrown by Methods

NumberFormatExceptionparseInt(String str) str does not contain a valid int.parseDouble(String str) str does not contain a valid double.valueOf(String str) str does not contain a valid value.

NullPointerExceptionString(String str) str is null.indexOf(String str) str is null.lastIndexOf(String str) str is null.

StringIndexOutOfBoundsException

charAt(int a) a is not a valid index.substring(int a) a is not a valid index.substring(int a, int b) a and/or b is not a valid index.

Exceptions Inheritance and Exception Handling 12/16

Page 111: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Exceptions Thrown by Methods

NumberFormatExceptionparseInt(String str) str does not contain a valid int.parseDouble(String str) str does not contain a valid double.valueOf(String str) str does not contain a valid value.

NullPointerExceptionString(String str) str is null.indexOf(String str) str is null.lastIndexOf(String str) str is null.

StringIndexOutOfBoundsException

charAt(int a) a is not a valid index.substring(int a) a is not a valid index.substring(int a, int b) a and/or b is not a valid index.

Exceptions Inheritance and Exception Handling 12/16

Page 112: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Exceptions Thrown by Methods

NumberFormatExceptionparseInt(String str) str does not contain a valid int.parseDouble(String str) str does not contain a valid double.valueOf(String str) str does not contain a valid value.

NullPointerExceptionString(String str) str is null.indexOf(String str) str is null.lastIndexOf(String str) str is null.

StringIndexOutOfBoundsException

charAt(int a) a is not a valid index.substring(int a) a is not a valid index.substring(int a, int b) a and/or b is not a valid index.

Exceptions Inheritance and Exception Handling 12/16

Page 113: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Exceptions Thrown by Methods

NumberFormatExceptionparseInt(String str) str does not contain a valid int.parseDouble(String str) str does not contain a valid double.valueOf(String str) str does not contain a valid value.

NullPointerExceptionString(String str) str is null.indexOf(String str) str is null.lastIndexOf(String str) str is null.

StringIndexOutOfBoundsException

charAt(int a) a is not a valid index.substring(int a) a is not a valid index.substring(int a, int b) a and/or b is not a valid index.

Exceptions Inheritance and Exception Handling 12/16

Page 114: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Exceptions Thrown by Methods

NumberFormatExceptionparseInt(String str) str does not contain a valid int.parseDouble(String str) str does not contain a valid double.valueOf(String str) str does not contain a valid value.

NullPointerExceptionString(String str) str is null.indexOf(String str) str is null.lastIndexOf(String str) str is null.

StringIndexOutOfBoundsException

charAt(int a) a is not a valid index.substring(int a) a is not a valid index.substring(int a, int b) a and/or b is not a valid index.

Exceptions Inheritance and Exception Handling 12/16

Page 115: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Exceptions Thrown by Methods

NumberFormatExceptionparseInt(String str) str does not contain a valid int.parseDouble(String str) str does not contain a valid double.valueOf(String str) str does not contain a valid value.

NullPointerExceptionString(String str) str is null.indexOf(String str) str is null.lastIndexOf(String str) str is null.

StringIndexOutOfBoundsException

charAt(int a) a is not a valid index.substring(int a) a is not a valid index.substring(int a, int b) a and/or b is not a valid index.

Exceptions Inheritance and Exception Handling 12/16

Page 116: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Checked Exceptions

Checked ExceptionAny exception that can be analysed by the compiler.Example: IOExceptions

Unchecked ExceptionsException that cannot be analysed when the program iscompiled.Must be checked for by programmer.Example: Division by zero.Example: Array index out of bounds.

Syntaxthrows 〈Exception1〉, 〈Exception2〉Attach modifier between the class/method declarationand the definition body.

Exceptions Inheritance and Exception Handling 13/16

Page 117: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Checked Exceptions

Checked ExceptionAny exception that can be analysed by the compiler.Example: IOExceptions

Unchecked ExceptionsException that cannot be analysed when the program iscompiled.Must be checked for by programmer.Example: Division by zero.Example: Array index out of bounds.

Syntaxthrows 〈Exception1〉, 〈Exception2〉Attach modifier between the class/method declarationand the definition body.

Exceptions Inheritance and Exception Handling 13/16

Page 118: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Checked Exceptions

Checked ExceptionAny exception that can be analysed by the compiler.Example: IOExceptions

Unchecked ExceptionsException that cannot be analysed when the program iscompiled.Must be checked for by programmer.Example: Division by zero.Example: Array index out of bounds.

Syntaxthrows 〈Exception1〉, 〈Exception2〉Attach modifier between the class/method declarationand the definition body.

Exceptions Inheritance and Exception Handling 13/16

Page 119: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Checked Exceptions

Checked ExceptionAny exception that can be analysed by the compiler.Example: IOExceptions

Unchecked ExceptionsException that cannot be analysed when the program iscompiled.Must be checked for by programmer.Example: Division by zero.Example: Array index out of bounds.

Syntaxthrows 〈Exception1〉, 〈Exception2〉Attach modifier between the class/method declarationand the definition body.

Exceptions Inheritance and Exception Handling 13/16

Page 120: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Checked Exceptions

Checked ExceptionAny exception that can be analysed by the compiler.Example: IOExceptions

Unchecked ExceptionsException that cannot be analysed when the program iscompiled.Must be checked for by programmer.Example: Division by zero.Example: Array index out of bounds.

Syntaxthrows 〈Exception1〉, 〈Exception2〉Attach modifier between the class/method declarationand the definition body.

Exceptions Inheritance and Exception Handling 13/16

Page 121: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Checked Exceptions

Checked ExceptionAny exception that can be analysed by the compiler.Example: IOExceptions

Unchecked ExceptionsException that cannot be analysed when the program iscompiled.Must be checked for by programmer.Example: Division by zero.Example: Array index out of bounds.

Syntaxthrows 〈Exception1〉, 〈Exception2〉Attach modifier between the class/method declarationand the definition body.

Exceptions Inheritance and Exception Handling 13/16

Page 122: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Checked Exceptions

Checked ExceptionAny exception that can be analysed by the compiler.Example: IOExceptions

Unchecked ExceptionsException that cannot be analysed when the program iscompiled.Must be checked for by programmer.Example: Division by zero.Example: Array index out of bounds.

Syntaxthrows 〈Exception1〉, 〈Exception2〉Attach modifier between the class/method declarationand the definition body.

Exceptions Inheritance and Exception Handling 13/16

Page 123: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Checked Exceptions

Checked ExceptionAny exception that can be analysed by the compiler.Example: IOExceptions

Unchecked ExceptionsException that cannot be analysed when the program iscompiled.Must be checked for by programmer.Example: Division by zero.Example: Array index out of bounds.

Syntaxthrows 〈Exception1〉, 〈Exception2〉Attach modifier between the class/method declarationand the definition body.

Exceptions Inheritance and Exception Handling 13/16

Page 124: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Checked Exceptions

Checked ExceptionAny exception that can be analysed by the compiler.Example: IOExceptions

Unchecked ExceptionsException that cannot be analysed when the program iscompiled.Must be checked for by programmer.Example: Division by zero.Example: Array index out of bounds.

Syntaxthrows 〈Exception1〉, 〈Exception2〉Attach modifier between the class/method declarationand the definition body.

Exceptions Inheritance and Exception Handling 13/16

Page 125: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Checked Exceptions

Checked ExceptionAny exception that can be analysed by the compiler.Example: IOExceptions

Unchecked ExceptionsException that cannot be analysed when the program iscompiled.Must be checked for by programmer.Example: Division by zero.Example: Array index out of bounds.

Syntaxthrows 〈Exception1〉, 〈Exception2〉Attach modifier between the class/method declarationand the definition body.

Exceptions Inheritance and Exception Handling 13/16

Page 126: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Checked Exceptions

Checked ExceptionAny exception that can be analysed by the compiler.Example: IOExceptions

Unchecked ExceptionsException that cannot be analysed when the program iscompiled.Must be checked for by programmer.Example: Division by zero.Example: Array index out of bounds.

Syntaxthrows 〈Exception1〉, 〈Exception2〉Attach modifier between the class/method declarationand the definition body.

Exceptions Inheritance and Exception Handling 13/16

Page 127: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Checked Exceptions

Checked ExceptionAny exception that can be analysed by the compiler.Example: IOExceptions

Unchecked ExceptionsException that cannot be analysed when the program iscompiled.Must be checked for by programmer.Example: Division by zero.Example: Array index out of bounds.

Syntaxthrows 〈Exception1〉, 〈Exception2〉Attach modifier between the class/method declarationand the definition body.

Exceptions Inheritance and Exception Handling 13/16

Page 128: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Exception Handling

try blockIncludes statements that may generate an exception.Includes statements that should not be executed if anexception occurs.Followed by zero or more catch blocks.

catch blockSpecifies type of exception it can catch.One catch bock for each type of exception.Can catch a specific exception or a category of exceptions.Contains the complete exception handler.Order of catch blocks is important – later blocks ignored.

finally blockCode is always executed whether exception occurs or not.Optional, except when no catch blocks are present.

Exception Handling Inheritance and Exception Handling 14/16

Page 129: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Exception Handling

try blockIncludes statements that may generate an exception.Includes statements that should not be executed if anexception occurs.Followed by zero or more catch blocks.

catch blockSpecifies type of exception it can catch.One catch bock for each type of exception.Can catch a specific exception or a category of exceptions.Contains the complete exception handler.Order of catch blocks is important – later blocks ignored.

finally blockCode is always executed whether exception occurs or not.Optional, except when no catch blocks are present.

Exception Handling Inheritance and Exception Handling 14/16

Page 130: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Exception Handling

try blockIncludes statements that may generate an exception.Includes statements that should not be executed if anexception occurs.Followed by zero or more catch blocks.

catch blockSpecifies type of exception it can catch.One catch bock for each type of exception.Can catch a specific exception or a category of exceptions.Contains the complete exception handler.Order of catch blocks is important – later blocks ignored.

finally blockCode is always executed whether exception occurs or not.Optional, except when no catch blocks are present.

Exception Handling Inheritance and Exception Handling 14/16

Page 131: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Exception Handling

try blockIncludes statements that may generate an exception.Includes statements that should not be executed if anexception occurs.Followed by zero or more catch blocks.

catch blockSpecifies type of exception it can catch.One catch bock for each type of exception.Can catch a specific exception or a category of exceptions.Contains the complete exception handler.Order of catch blocks is important – later blocks ignored.

finally blockCode is always executed whether exception occurs or not.Optional, except when no catch blocks are present.

Exception Handling Inheritance and Exception Handling 14/16

Page 132: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Exception Handling

try blockIncludes statements that may generate an exception.Includes statements that should not be executed if anexception occurs.Followed by zero or more catch blocks.

catch blockSpecifies type of exception it can catch.One catch bock for each type of exception.Can catch a specific exception or a category of exceptions.Contains the complete exception handler.Order of catch blocks is important – later blocks ignored.

finally blockCode is always executed whether exception occurs or not.Optional, except when no catch blocks are present.

Exception Handling Inheritance and Exception Handling 14/16

Page 133: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Exception Handling

try blockIncludes statements that may generate an exception.Includes statements that should not be executed if anexception occurs.Followed by zero or more catch blocks.

catch blockSpecifies type of exception it can catch.One catch bock for each type of exception.Can catch a specific exception or a category of exceptions.Contains the complete exception handler.Order of catch blocks is important – later blocks ignored.

finally blockCode is always executed whether exception occurs or not.Optional, except when no catch blocks are present.

Exception Handling Inheritance and Exception Handling 14/16

Page 134: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Exception Handling

try blockIncludes statements that may generate an exception.Includes statements that should not be executed if anexception occurs.Followed by zero or more catch blocks.

catch blockSpecifies type of exception it can catch.One catch bock for each type of exception.Can catch a specific exception or a category of exceptions.Contains the complete exception handler.Order of catch blocks is important – later blocks ignored.

finally blockCode is always executed whether exception occurs or not.Optional, except when no catch blocks are present.

Exception Handling Inheritance and Exception Handling 14/16

Page 135: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Exception Handling

try blockIncludes statements that may generate an exception.Includes statements that should not be executed if anexception occurs.Followed by zero or more catch blocks.

catch blockSpecifies type of exception it can catch.One catch bock for each type of exception.Can catch a specific exception or a category of exceptions.Contains the complete exception handler.Order of catch blocks is important – later blocks ignored.

finally blockCode is always executed whether exception occurs or not.Optional, except when no catch blocks are present.

Exception Handling Inheritance and Exception Handling 14/16

Page 136: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Exception Handling

try blockIncludes statements that may generate an exception.Includes statements that should not be executed if anexception occurs.Followed by zero or more catch blocks.

catch blockSpecifies type of exception it can catch.One catch bock for each type of exception.Can catch a specific exception or a category of exceptions.Contains the complete exception handler.Order of catch blocks is important – later blocks ignored.

finally blockCode is always executed whether exception occurs or not.Optional, except when no catch blocks are present.

Exception Handling Inheritance and Exception Handling 14/16

Page 137: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Exception Handling

try blockIncludes statements that may generate an exception.Includes statements that should not be executed if anexception occurs.Followed by zero or more catch blocks.

catch blockSpecifies type of exception it can catch.One catch bock for each type of exception.Can catch a specific exception or a category of exceptions.Contains the complete exception handler.Order of catch blocks is important – later blocks ignored.

finally blockCode is always executed whether exception occurs or not.Optional, except when no catch blocks are present.

Exception Handling Inheritance and Exception Handling 14/16

Page 138: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Exception Handling

try blockIncludes statements that may generate an exception.Includes statements that should not be executed if anexception occurs.Followed by zero or more catch blocks.

catch blockSpecifies type of exception it can catch.One catch bock for each type of exception.Can catch a specific exception or a category of exceptions.Contains the complete exception handler.Order of catch blocks is important – later blocks ignored.

finally blockCode is always executed whether exception occurs or not.Optional, except when no catch blocks are present.

Exception Handling Inheritance and Exception Handling 14/16

Page 139: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Exception Handling

try blockIncludes statements that may generate an exception.Includes statements that should not be executed if anexception occurs.Followed by zero or more catch blocks.

catch blockSpecifies type of exception it can catch.One catch bock for each type of exception.Can catch a specific exception or a category of exceptions.Contains the complete exception handler.Order of catch blocks is important – later blocks ignored.

finally blockCode is always executed whether exception occurs or not.Optional, except when no catch blocks are present.

Exception Handling Inheritance and Exception Handling 14/16

Page 140: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Exception Handling

try blockIncludes statements that may generate an exception.Includes statements that should not be executed if anexception occurs.Followed by zero or more catch blocks.

catch blockSpecifies type of exception it can catch.One catch bock for each type of exception.Can catch a specific exception or a category of exceptions.Contains the complete exception handler.Order of catch blocks is important – later blocks ignored.

finally blockCode is always executed whether exception occurs or not.Optional, except when no catch blocks are present.

Exception Handling Inheritance and Exception Handling 14/16

Page 141: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Exception Handling

try blockIncludes statements that may generate an exception.Includes statements that should not be executed if anexception occurs.Followed by zero or more catch blocks.

catch blockSpecifies type of exception it can catch.One catch bock for each type of exception.Can catch a specific exception or a category of exceptions.Contains the complete exception handler.Order of catch blocks is important – later blocks ignored.

finally blockCode is always executed whether exception occurs or not.Optional, except when no catch blocks are present.

Exception Handling Inheritance and Exception Handling 14/16

Page 142: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Exception Handling Techniques

Terminate programOutput an appropriate error message and terminate.

Fix error and continueGenerate an appropriate error message, wait for userinput and try again (repeatedly until a valid input isobtained).

Log error and continueWrite error messages (to a logfile) and continue withprogram execution.

RethrowIf unable to process the exception.If exception should be handled by calling environment.Default action if exception is not caught.

Exception Handling Inheritance and Exception Handling 15/16

Page 143: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Exception Handling Techniques

Terminate programOutput an appropriate error message and terminate.

Fix error and continueGenerate an appropriate error message, wait for userinput and try again (repeatedly until a valid input isobtained).

Log error and continueWrite error messages (to a logfile) and continue withprogram execution.

RethrowIf unable to process the exception.If exception should be handled by calling environment.Default action if exception is not caught.

Exception Handling Inheritance and Exception Handling 15/16

Page 144: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Exception Handling Techniques

Terminate programOutput an appropriate error message and terminate.

Fix error and continueGenerate an appropriate error message, wait for userinput and try again (repeatedly until a valid input isobtained).

Log error and continueWrite error messages (to a logfile) and continue withprogram execution.

RethrowIf unable to process the exception.If exception should be handled by calling environment.Default action if exception is not caught.

Exception Handling Inheritance and Exception Handling 15/16

Page 145: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Exception Handling Techniques

Terminate programOutput an appropriate error message and terminate.

Fix error and continueGenerate an appropriate error message, wait for userinput and try again (repeatedly until a valid input isobtained).

Log error and continueWrite error messages (to a logfile) and continue withprogram execution.

RethrowIf unable to process the exception.If exception should be handled by calling environment.Default action if exception is not caught.

Exception Handling Inheritance and Exception Handling 15/16

Page 146: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Exception Handling Techniques

Terminate programOutput an appropriate error message and terminate.

Fix error and continueGenerate an appropriate error message, wait for userinput and try again (repeatedly until a valid input isobtained).

Log error and continueWrite error messages (to a logfile) and continue withprogram execution.

RethrowIf unable to process the exception.If exception should be handled by calling environment.Default action if exception is not caught.

Exception Handling Inheritance and Exception Handling 15/16

Page 147: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Exception Handling Techniques

Terminate programOutput an appropriate error message and terminate.

Fix error and continueGenerate an appropriate error message, wait for userinput and try again (repeatedly until a valid input isobtained).

Log error and continueWrite error messages (to a logfile) and continue withprogram execution.

RethrowIf unable to process the exception.If exception should be handled by calling environment.Default action if exception is not caught.

Exception Handling Inheritance and Exception Handling 15/16

Page 148: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Exception Handling Techniques

Terminate programOutput an appropriate error message and terminate.

Fix error and continueGenerate an appropriate error message, wait for userinput and try again (repeatedly until a valid input isobtained).

Log error and continueWrite error messages (to a logfile) and continue withprogram execution.

RethrowIf unable to process the exception.If exception should be handled by calling environment.Default action if exception is not caught.

Exception Handling Inheritance and Exception Handling 15/16

Page 149: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Exception Handling Techniques

Terminate programOutput an appropriate error message and terminate.

Fix error and continueGenerate an appropriate error message, wait for userinput and try again (repeatedly until a valid input isobtained).

Log error and continueWrite error messages (to a logfile) and continue withprogram execution.

RethrowIf unable to process the exception.If exception should be handled by calling environment.Default action if exception is not caught.

Exception Handling Inheritance and Exception Handling 15/16

Page 150: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Exception Handling Techniques

Terminate programOutput an appropriate error message and terminate.

Fix error and continueGenerate an appropriate error message, wait for userinput and try again (repeatedly until a valid input isobtained).

Log error and continueWrite error messages (to a logfile) and continue withprogram execution.

RethrowIf unable to process the exception.If exception should be handled by calling environment.Default action if exception is not caught.

Exception Handling Inheritance and Exception Handling 15/16

Page 151: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Exception Handling Techniques

Terminate programOutput an appropriate error message and terminate.

Fix error and continueGenerate an appropriate error message, wait for userinput and try again (repeatedly until a valid input isobtained).

Log error and continueWrite error messages (to a logfile) and continue withprogram execution.

RethrowIf unable to process the exception.If exception should be handled by calling environment.Default action if exception is not caught.

Exception Handling Inheritance and Exception Handling 15/16

Page 152: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Exception Handling Techniques

Terminate programOutput an appropriate error message and terminate.

Fix error and continueGenerate an appropriate error message, wait for userinput and try again (repeatedly until a valid input isobtained).

Log error and continueWrite error messages (to a logfile) and continue withprogram execution.

RethrowIf unable to process the exception.If exception should be handled by calling environment.Default action if exception is not caught.

Exception Handling Inheritance and Exception Handling 15/16

Page 153: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Creating Your Own Exception Classes

Allows the class to provide additional information to thecaller as to the cause of the exception.

Caller can analyse the additional information and respondaccordingly.

Exception Definition

Extend the Exception class or one of its subclassespublic class NodeNotFoundException extends Exception {

public NodeNotFoundException() {super("NodeNotFoundExcpetion");

}}

Throwing an Exception

The syntax to throw your own exception is:throw new NodeNotFoundException();

Custom Exceptions Inheritance and Exception Handling 16/16

Page 154: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Creating Your Own Exception Classes

Allows the class to provide additional information to thecaller as to the cause of the exception.

Caller can analyse the additional information and respondaccordingly.

Exception Definition

Extend the Exception class or one of its subclassespublic class NodeNotFoundException extends Exception {

public NodeNotFoundException() {super("NodeNotFoundExcpetion");

}}

Throwing an Exception

The syntax to throw your own exception is:throw new NodeNotFoundException();

Custom Exceptions Inheritance and Exception Handling 16/16

Page 155: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Creating Your Own Exception Classes

Allows the class to provide additional information to thecaller as to the cause of the exception.

Caller can analyse the additional information and respondaccordingly.

Exception Definition

Extend the Exception class or one of its subclassespublic class NodeNotFoundException extends Exception {

public NodeNotFoundException() {super("NodeNotFoundExcpetion");

}}

Throwing an Exception

The syntax to throw your own exception is:throw new NodeNotFoundException();

Custom Exceptions Inheritance and Exception Handling 16/16

Page 156: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Creating Your Own Exception Classes

Allows the class to provide additional information to thecaller as to the cause of the exception.

Caller can analyse the additional information and respondaccordingly.

Exception Definition

Extend the Exception class or one of its subclassespublic class NodeNotFoundException extends Exception {

public NodeNotFoundException() {super("NodeNotFoundExcpetion");

}}

Throwing an Exception

The syntax to throw your own exception is:throw new NodeNotFoundException();

Custom Exceptions Inheritance and Exception Handling 16/16

Page 157: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Creating Your Own Exception Classes

Allows the class to provide additional information to thecaller as to the cause of the exception.

Caller can analyse the additional information and respondaccordingly.

Exception Definition

Extend the Exception class or one of its subclassespublic class NodeNotFoundException extends Exception {

public NodeNotFoundException() {super("NodeNotFoundExcpetion");

}}

Throwing an Exception

The syntax to throw your own exception is:throw new NodeNotFoundException();

Custom Exceptions Inheritance and Exception Handling 16/16

Page 158: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Creating Your Own Exception Classes

Allows the class to provide additional information to thecaller as to the cause of the exception.

Caller can analyse the additional information and respondaccordingly.

Exception Definition

Extend the Exception class or one of its subclassespublic class NodeNotFoundException extends Exception {

public NodeNotFoundException() {super("NodeNotFoundExcpetion");

}}

Throwing an Exception

The syntax to throw your own exception is:throw new NodeNotFoundException();

Custom Exceptions Inheritance and Exception Handling 16/16

Page 159: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Creating Your Own Exception Classes

Allows the class to provide additional information to thecaller as to the cause of the exception.

Caller can analyse the additional information and respondaccordingly.

Exception Definition

Extend the Exception class or one of its subclassespublic class NodeNotFoundException extends Exception {

public NodeNotFoundException() {super("NodeNotFoundExcpetion");

}}

Throwing an Exception

The syntax to throw your own exception is:throw new NodeNotFoundException();

Custom Exceptions Inheritance and Exception Handling 16/16

Page 160: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Creating Your Own Exception Classes

Allows the class to provide additional information to thecaller as to the cause of the exception.

Caller can analyse the additional information and respondaccordingly.

Exception Definition

Extend the Exception class or one of its subclassespublic class NodeNotFoundException extends Exception {

public NodeNotFoundException() {super("NodeNotFoundExcpetion");

}}

Throwing an Exception

The syntax to throw your own exception is:throw new NodeNotFoundException();

Custom Exceptions Inheritance and Exception Handling 16/16

Page 161: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Creating Your Own Exception Classes

Allows the class to provide additional information to thecaller as to the cause of the exception.

Caller can analyse the additional information and respondaccordingly.

Exception Definition

Extend the Exception class or one of its subclassespublic class NodeNotFoundException extends Exception {

public NodeNotFoundException() {super("NodeNotFoundExcpetion");

}}

Throwing an Exception

The syntax to throw your own exception is:throw new NodeNotFoundException();

Custom Exceptions Inheritance and Exception Handling 16/16

Page 162: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Creating Your Own Exception Classes

Allows the class to provide additional information to thecaller as to the cause of the exception.

Caller can analyse the additional information and respondaccordingly.

Exception Definition

Extend the Exception class or one of its subclassespublic class NodeNotFoundException extends Exception {

public NodeNotFoundException() {super("NodeNotFoundExcpetion");

}}

Throwing an Exception

The syntax to throw your own exception is:throw new NodeNotFoundException();

Custom Exceptions Inheritance and Exception Handling 16/16

Page 163: Inheritance and Exception Handlingempslocal.ex.ac.uk/people/staff/pjk205/ECM1406/slides02.pdf · 2011-01-19 · Inheritance and Exception Handling Inheritance and Exception Handling

Creating Your Own Exception Classes

Allows the class to provide additional information to thecaller as to the cause of the exception.

Caller can analyse the additional information and respondaccordingly.

Exception Definition

Extend the Exception class or one of its subclassespublic class NodeNotFoundException extends Exception {

public NodeNotFoundException() {super("NodeNotFoundExcpetion");

}}

Throwing an Exception

The syntax to throw your own exception is:throw new NodeNotFoundException();

Custom Exceptions Inheritance and Exception Handling 16/16