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

Slides:



Advertisements
Similar presentations
Topics Introduction Types of Errors Exceptions Exception Handling
Advertisements

Exception Handling. Introduction Errors can be dealt with at place error occurs –Easy to see if proper error checking implemented –Harder to read application.
Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net.
Exception Handling Chapter 15 2 What You Will Learn Use try, throw, catch to watch for indicate exceptions handle How to process exceptions and failures.
Introduction to Exceptions in Java. 2 Runtime Errors What are syntax errors? What are runtime errors? Java differentiates between runtime errors and exceptions.
Errors and Exceptions The objectives of this chapter are: To understand the exception handling mechanism defined in Java To explain the difference between.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 15 – Exception Handling Outline 15.1 Introduction 15.2 Exception-Handling Overview 15.3 Exception-Handling.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Chapter 16: Exception Handling C++ Programming: From Problem Analysis to Program Design, Fifth Edition.
Objectives In this chapter you will: Learn what an exception is Learn how to handle exceptions within a program See how a try / catch block is used to.
Exception Handling Yaodong Bi Exception Handling Java exception handling Try blocks Throwing and re-throwing an exception Catching an.
MIT-AITI Lecture 14: Exceptions Handling Errors with Exceptions Kenya 2005.
© The McGraw-Hill Companies, 2006 Chapter 15. © The McGraw-Hill Companies, 2006 Exceptions an exception is an event that occurs during the life of a program.
 2002 Prentice Hall, Inc. All rights reserved. Chapter 14 – Exception Handling Outline 14.1 Introduction 14.2 When Exception Handling Should Be Used 14.3.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
Objectives Understanding what an exception is Understanding the heirarchy of exception classes Learn the types of exception and how to catch and handle.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 14 - Exception Handling Outline 14.1Introduction 14.2When Exception Handling Should Be Used 14.3Other.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exceptions Handling.
Exceptions Three categories of errors: Syntax errors Runtime errors Logic errors Syntax errors: rules of the language have not been followed. Runtime error:
16-Jun-15 Exceptions. Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a null.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
© Copyright Eliyahu Brutman Exceptions. © Copyright Eliyahu Brutman Exceptions and Design Patterns - 2 Introduction to Exception Handling Definition:
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Chapter 8  Errors and Exceptions Throwable class.
1 Exception Handling  Introduction to Exceptions  How exceptions are generated  A partial hierarchy of Java exceptions  Checked and Unchecked Exceptions.
Java Review 2 – Errors, Exceptions, Debugging Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 11: Handling Exceptions and Events J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Fourth.
Java Software Solutions Foundations of Program Design Sixth Edition
What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic.
1 Chapter 14 - Exception Handling Outline 14.1Introduction 14.2When Exception Handling Should Be Used 14.3Other Error Handling Techniques 14.4The Basics.
Chapter 12 Inheritance and Exceptions Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
06 Exception Handling. 2 Contents What is an Exception? Exception-handling in Java Types of Exceptions Exception Hierarchy try-catch()-finally Statement.
CIS 270—Application Development II Chapter 13—Exception Handling.
Chapter 12: Exception Handling
Exception Handling in Java Exception Handling Introduction: After completing this chapter, you will be able to comprehend the nature and kinds.
Slides Credit Umair Javed LUMS Web Application Development.
Exceptions Handling the unexpected. RHS – SWC 2 The Real World So far, most of our code has been somewhat näive We have assumed that nothing goes wrong…
Java Programming: Guided Learning with Early Objects
COMP Exception Handling Yi Hong June 10, 2015.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
Exception Handling Unit-6. Introduction An exception is a problem that arises during the execution of a program. An exception can occur for many different.
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Data Structures Using Java1 Chapter 2 Inheritance and Exception Handling.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 11 Handling Exceptions and Events.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 14 - Exception Handling Outline 14.1Introduction 14.2When Exception Handling Should Be Used 14.3Other.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
Exceptions Handling Prepared by: Ligemm Mae del Castillo.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Exception. Agenda Exception. Handling Exceptions. The finally Clause.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Introduction to Exceptions in Java CS201, SW Development Methods.
Chapter 14 – Exception Handling
Chapter 14: Exception Handling
Exception Handling Chapter 9.
Exception Handling Chapter 9 Edited by JJ.
Web Design & Development Lecture 7
Lecture 11 Objectives Learn what an exception is.
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 15 – Exception Handling
Presentation transcript:

Chapter 12 Handling Exceptions and Events

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

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

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

Java Exception Hierarchy

Constructors and Methods of the class Throwable

The class Exception and its Subclasses from java.lang

The class Exception and its Subclasses from java.util

The class Exception and its Subclasses from java.io

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

The class Exception and its Constructors

Java Exception Classes

Exceptions Thrown by Methods

Checked Exceptions Definition: any exception that can be analyzed by the compiler Examples –IOExceptions

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

Exceptions Example Code public static void exceptionMethod() throws NumberFormatException, IOException { //statements } The method exceptionMethod throws exceptions of the type NumberFormatException and IOException.

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

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

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 }

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

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

Combining catch Blocks With the Operator instanceof

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

Rethrowing and Throwing an Exception

The Method printStackTrace Used to determine the order in which the methods were called and where the exception was handled

The Method printStackTrace

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

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);

Creating Your Own Exception Classes

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

Event Handling class WindowAdapter –implements interface WindowListener with empty bodies to methods class MouseAdapter –implements interface MouseListener with empty bodies to methods

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

Event Handling

Programming Example: Calculator

Chapter Summary Exception –Definition –Syntax –Hierarchy –Classes Checked and Unchecked Exceptions The method printStackTrace Creating your own exception classes Event Handling

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