39
Chapter 12 Handling Exceptions and Events

Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about

Embed Size (px)

Citation preview

Page 1: Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about

Chapter 12

Handling Exceptions and Events

Page 2: Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about

Chapter Objectives

• Learn what an exception is

• Become aware of the hierarchy of exception classes

• Learn about checked and unchecked exceptions

• Learn how to handle exceptions within a program

Page 3: Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about

Chapter Objectives

• See how a try/catch block is used to handle exceptions

• Discover how to throw and rethrow an exception

• Learn how to handle events in a program

Page 4: Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about

Exception

• Definition: an occurrence of an undesirable situation that can be detected during program execution

• Examples– division by zero– trying to open an input file that does not exist– an array index that goes out of bounds

Page 5: Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about

Java Exception Hierarchy

Page 6: Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about

Constructors and Methods of the class Throwable

Page 7: Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about

The class Exception and its Subclasses from java.lang

Page 8: Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about

The class Exception and its Subclasses from java.util

Page 9: Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about

The class Exception and its Subclasses from java.io

Page 10: Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about

Java’s Exception Class• class Exception

– Subclass of class Throwable – Superclass of classes designed to handle exceptions

• Various types of exceptions– I/O exceptions– Number format exceptions– File not found exceptions– Array index out of bounds exceptions

• Various exceptions categorized into separate classes and contained in various packages

Page 11: Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about

The class Exception and its Constructors

Page 12: Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about

Java Exception Classes

Page 13: Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about

Exceptions Thrown by Methods

Page 14: Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about

Exceptions Thrown by Methods

Page 15: Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about

Checked Exceptions

• Definition: any exception that can be analyzed by the compiler

• Examples– IOExceptions

Page 16: Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about

Unchecked Exceptions• Definition: exceptions that cannot be analyzed

when the program compiles (must be checked for by programmer)

• Examples– Division by zero– Array index out of bounds

• Syntax– throws ExceptionType1, ExceptionType2, …

*ExceptionType1, ExceptionType2, etc are names of exception classes

Page 17: Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about

Exceptions Example Code

public static void exceptionMethod()

throws NumberFormatException, IOException

{

//statements

}

• The method exceptionMethod throws exceptions of the type NumberFormatException and IOException.

Page 18: Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about

Handling Exceptions within a Program

• try/catch/finally block used to handle exceptions within a program

• try block:– Includes statements that may generate an exception

– Includes statements that should not be executed if an exception occurs

– Followed by zero or more catch blocks

– May or may not be followed by finally block

Page 19: Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about

Handling Exceptions within a Program

• catch block:– Heading specifies type of exception it can catch– Contains exception handler; completely handles

exception– Can catch all exceptions of a specific type or all types

of exceptions– May or may not be followed by a finally block

• finally block:– Code contained in this block always executes– try block with no catch block has finally block

Page 20: Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about

Handling Exceptions within a Programtry{ //statements}catch(ExceptionClassName1 objRef1){ //exception handler code}catch(ExceptionClassName2 objRef2){ //exception handler code}...catch(ExceptionClassNameN objRefN){ //exception handler code}finally{ //statements}

Page 21: Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about

Order of catch Blocks

• If an exception in a try block is caught by the first catch block, the remaining catch blocks are ignored

• Must be careful about order catch blocks are listed

Page 22: Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about

The class Exception and the Operator instanceof

• A reference of a superclass type can point to objects of its subclass

• You can determine if a reference variable points to an object using the operator instanceof

• You can combine catch blocks using this facility

Page 23: Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about

Combining catch Blocks With the Operator instanceof

Page 24: Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about

Rethrowing and Throwing an Exception

• Useful when – catch block catches exception but is unable to

handle it– catch block decides exception should be

handled by calling environment

• Allows programmer to provide exception handling code in one place

Page 25: Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about

Rethrowing and Throwing an Exception

Page 26: Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about

Rethrowing and Throwing an Exception

Page 27: Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about

The Method printStackTrace

• Used to determine the order in which the methods were called and where the exception was handled

Page 28: Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about

The Method printStackTrace

Page 29: Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about

The Method printStackTrace

Page 30: Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about

Exception-Handling Techniques

• Terminate program– Output appropriate error message upon termination

• Fix error and continue– Repeatedly get user input

– Output appropriate error message until valid value is entered

• Log error and continue– Write error messages to file and continue with program

execution

Page 31: Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about

Creating Your Own Exception Classes

• Exception class you define extends class Exception or one of its subclasses

• Syntax to throw your own exception object– throw new ExceptionClassName(messageString);

Page 32: Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about

Creating Your Own Exception Classes

Page 33: Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about

Event Handling

• Action events – handled by implementing interface ActionListener

• Window events – handled by implementing interface WindowListener

• Mouse events – handled by implementing interface MouseListener

• Key events – handled by implementing interface KeyListener

Page 34: Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about

Event Handling

• class WindowAdapter – implements interface WindowListener with

empty bodies to methods

• class MouseAdapter – implements interface MouseListener with

empty bodies to methods

Page 35: Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about

Registering Listeners

• Registering window listener object to GUI component:– use method addWindowListener– window listener object being registered is passed as

parameter to method addWindowListener

• Registering mouse listener object to GUI component:– use method addMouseListener– mouse listener object being registered is passed as

parameter to method addMouseListener

Page 36: Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about

Event Handling

Page 37: Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about

Programming Example: Calculator

Page 38: Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about

Chapter Summary

• Exception– Definition– Syntax– Hierarchy – Classes

• Checked and Unchecked Exceptions• The method printStackTrace• Creating your own exception classes• Event Handling

Page 39: Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about

Chapter Summary

• Exception handling techniques– Terminate program– Fix error and continue– Log error and continue

• Handling exceptions within a program– try/catch/finally block– Order of catch blocks– Using try/catch blocks in a program– The class Exception and the Operator instanceof– Rethrowing and throwing an exception