Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become...
-
Upload
alfred-woods -
Category
Documents
-
view
226 -
download
4
Transcript of Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become...
![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.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1d5503460f94c342b4/html5/thumbnails/1.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1d5503460f94c342b4/html5/thumbnails/2.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1d5503460f94c342b4/html5/thumbnails/3.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1d5503460f94c342b4/html5/thumbnails/4.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1d5503460f94c342b4/html5/thumbnails/5.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1d5503460f94c342b4/html5/thumbnails/6.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1d5503460f94c342b4/html5/thumbnails/7.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1d5503460f94c342b4/html5/thumbnails/8.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1d5503460f94c342b4/html5/thumbnails/9.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1d5503460f94c342b4/html5/thumbnails/10.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1d5503460f94c342b4/html5/thumbnails/11.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1d5503460f94c342b4/html5/thumbnails/12.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1d5503460f94c342b4/html5/thumbnails/13.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1d5503460f94c342b4/html5/thumbnails/14.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1d5503460f94c342b4/html5/thumbnails/15.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1d5503460f94c342b4/html5/thumbnails/16.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1d5503460f94c342b4/html5/thumbnails/17.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1d5503460f94c342b4/html5/thumbnails/18.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1d5503460f94c342b4/html5/thumbnails/19.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1d5503460f94c342b4/html5/thumbnails/20.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1d5503460f94c342b4/html5/thumbnails/21.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1d5503460f94c342b4/html5/thumbnails/22.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1d5503460f94c342b4/html5/thumbnails/23.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1d5503460f94c342b4/html5/thumbnails/24.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1d5503460f94c342b4/html5/thumbnails/25.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1d5503460f94c342b4/html5/thumbnails/26.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1d5503460f94c342b4/html5/thumbnails/27.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1d5503460f94c342b4/html5/thumbnails/28.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1d5503460f94c342b4/html5/thumbnails/29.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1d5503460f94c342b4/html5/thumbnails/30.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1d5503460f94c342b4/html5/thumbnails/31.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1d5503460f94c342b4/html5/thumbnails/32.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1d5503460f94c342b4/html5/thumbnails/33.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1d5503460f94c342b4/html5/thumbnails/34.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1d5503460f94c342b4/html5/thumbnails/35.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1d5503460f94c342b4/html5/thumbnails/36.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1d5503460f94c342b4/html5/thumbnails/37.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1d5503460f94c342b4/html5/thumbnails/38.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1d5503460f94c342b4/html5/thumbnails/39.jpg)
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