Download presentation
Presentation is loading. Please wait.
Published byAlfred Woods Modified over 9 years ago
1
Chapter 12 Handling Exceptions and Events
2
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
3
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
4
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
5
Java Exception Hierarchy
6
Constructors and Methods of the class Throwable
7
The class Exception and its Subclasses from java.lang
8
The class Exception and its Subclasses from java.util
9
The class Exception and its Subclasses from java.io
10
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
11
The class Exception and its Constructors
12
Java Exception Classes
13
Exceptions Thrown by Methods
15
Checked Exceptions Definition: any exception that can be analyzed by the compiler Examples –IOExceptions
16
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
17
Exceptions Example Code public static void exceptionMethod() throws NumberFormatException, IOException { //statements } The method exceptionMethod throws exceptions of the type NumberFormatException and IOException.
18
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
19
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
20
Handling Exceptions within a Program try { //statements } catch(ExceptionClassName1 objRef1) { //exception handler code } catch(ExceptionClassName2 objRef2) { //exception handler code }... catch(ExceptionClassNameN objRefN) { //exception handler code } finally { //statements }
21
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
22
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
23
Combining catch Blocks With the Operator instanceof
24
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
25
Rethrowing and Throwing an Exception
27
The Method printStackTrace Used to determine the order in which the methods were called and where the exception was handled
28
The Method printStackTrace
30
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
31
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);
32
Creating Your Own Exception Classes
33
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
34
Event Handling class WindowAdapter –implements interface WindowListener with empty bodies to methods class MouseAdapter –implements interface MouseListener with empty bodies to methods
35
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
36
Event Handling
37
Programming Example: Calculator
38
Chapter Summary Exception –Definition –Syntax –Hierarchy –Classes Checked and Unchecked Exceptions The method printStackTrace Creating your own exception classes Event Handling
39
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.