Chapter 11: Exception Handling

Slides:



Advertisements
Similar presentations
Pearson Education, Inc. All rights reserved. 1.. Exception Handling.
Advertisements

Topics Introduction Types of Errors Exceptions Exception Handling
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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.
Review Linked list: Doubly linked list, insertback, insertbefore Remove Search.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 13 Exception Handling.
Try…Catch…Finally Blocks ( continued ) Generic catch clause –Omit argument list with the catch –Any exception thrown is handled by executing code within.
C# Programming: From Problem Analysis to Program Design1 Debugging and Handling Exceptions C# Programming: From Problem Analysis to Program Design 3 rd.
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.
Chapter 11 Debugging and Handling Exceptions
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.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling.
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
Slides prepared by Rose Williams, Binghamton University Chapter 9 Exception Handling.
Exception Handling An Exception is an indication of a problem that occurs during a program’s execution. Exception handling enables the programmer to create.
Exception Error handling. Exception 4 n An unusual occurrence during program execution that requires immediate handling n Errors are the most common type.
1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about.
Object Oriented Programming
Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch.
CIS 270—Application Development II Chapter 13—Exception Handling.
Chapter 12: Exception Handling
COMPUTER PROGRAMMING 2 Exceptions. What are Exceptions? Unexpected events that happen when the code is executing (during runtime). Exceptions are types.
VB.Net - Exceptions Copyright © Martin Schray
Introduction to Exception Handling and Defensive Programming.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
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.
HANDLING EXCEPTIONS Chapter 9. Outline  Learn about the limitations of traditional error-handling methods  Throw exceptions  Use try blocks  Catch.
1 An Exception is… An unusual, often unpredictable event, detectable by software or hardware, that requires special processing An exception handler is.
Exceptions and Assertions Chapter 15 – CSCI 1302.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling.
Copyright © Curt Hill Error Handling in Java Throwing and catching exceptions.
Exception Handling SWE 344 Internet Protocols & Client Server Programming.
Exception and Exception Handling. Exception An abnormal event that is likely to happen during program is execution Computer could run out of memory Calling.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Lecture 18B Exception Handling and Richard Gesick.
Generics, Exception and Undo Command. A Generic Stack Class AStack uses generic. In the main method, we have 2 instances of AStack, each type is Integer.
Lec.11 (Chapter 11) Exception Jiang (Jen) ZHENG July 13 th, 2005.
16 Exception Handling.
Debugging and Handling Exceptions
Java Programming Fifth Edition
Why exception handling in C++?
Chapter 14: Exception Handling
Exception Handling and
CNS 3260 C# .NET Software Development
Exception Handling Chapter 9.
Example: Finding the Mode
Chapter 12 Exception Handling and Text IO
Chapter 12 Exception Handling
Exceptions Problems in a Java program may cause exceptions or errors representing unusual or invalid processing. An exception is an object that defines.
Exception Handling in Java
Exception Handling and Reading / Writing Files
Exception Handling Chapter 9 Edited by JJ.
Exception Handling.
Exception Handling Oo28.
Part B – Structured Exception Handling
Exception Handling By: Enas Naffar.
Programming in C# Lesson 5. Exceptions..
Exception Handling Imran Rashid CTO at ManiWeber Technologies.
Microsoft Visual Basic 2005 BASICS
Object-Oriented Programming (OOP) Lecture No. 43
Introduction to Programming
Debugging and Handling Exceptions
Exception Handling.
Presentation transcript:

Chapter 11: Exception Handling

Understanding Exceptions Any error condition or unexpected behavior in an executing program Exception handling Object-oriented techniques used to manage such errors Exceptions are objects of the Exception class or one of its derived classes Microsoft Visual C# 2012, Fifth Edition

Microsoft Visual C# 2012, Fifth Edition

Purposely Generating a SystemException You can deliberately generate a SystemException by forcing a program to contain an error Example: Dividing an integer by zero You don’t necessarily have to deal with exceptions Termination of the program is abrupt and unforgiving Object-oriented error-handling techniques provide more elegant solutions Microsoft Visual C# 2012, Fifth Edition

Purposely Generating a SystemException (cont’d.) Microsoft Visual C# 2012, Fifth Edition

Purposely Generating a SystemException (cont’d.) Microsoft Visual C# 2012, Fifth Edition

Understanding Traditional and Object-Oriented Error-Handling Methods Check a variable’s value with an if statement before attempting to divide it into another number Prevents division by zero However, it does not really “handle an exception” Is efficient if you think it will be a frequent problem Has little “overhead” Otherwise, create an Exception object Microsoft Visual C# 2012, Fifth Edition

Understanding Object-Oriented Exception-Handling Methods try block Contains statements that can produce an error Code at least one catch block or finally block immediately following a try block catch block Can “catch” one type of Exception Microsoft Visual C# 2012, Fifth Edition

Understanding Object-Oriented Exception-Handling Methods (cont’d.) Microsoft Visual C# 2012, Fifth Edition

Understanding Object-Oriented Exception-Handling Methods (cont’d.) Microsoft Visual C# 2012, Fifth Edition

Understanding Object-Oriented Exception-Handling Methods (cont’d.) Microsoft Visual C# 2012, Fifth Edition

Using the Exception Class’s ToString() Method and Message Property Using The Exception class and ToString() Provides a descriptive error message The user can receive precise information about the nature of any Exception that is thrown Microsoft Visual C# 2012, Fifth Edition

Using the Exception Class’s ToString() Method and Message Property (cont’d.) Microsoft Visual C# 2012, Fifth Edition

Using the Exception Class’s ToString() Method and Message Property (cont’d.) Microsoft Visual C# 2012, Fifth Edition

Catching Multiple Exceptions You can place as many statements as you need within a try block Only the first error-generating statement throws an Exception Multiple catch blocks are examined in sequence until a match is found for the Exception that occurred Various Exceptions can be handled by the same catch block Microsoft Visual C# 2012, Fifth Edition

Catching Multiple Exceptions (cont’d.) Microsoft Visual C# 2012, Fifth Edition

Catching Multiple Exceptions (cont’d.) Microsoft Visual C# 2012, Fifth Edition

Catching Multiple Exceptions (cont’d.) Microsoft Visual C# 2012, Fifth Edition

Catching Multiple Exceptions (cont’d.) Microsoft Visual C# 2012, Fifth Edition

Catching Multiple Exceptions (cont’d.) Microsoft Visual C# 2012, Fifth Edition

Catching Multiple Exceptions (cont’d.) It is poor coding style for a method to throw more than three or four types of Exceptions The method is trying to accomplish too many diverse tasks The Exception types thrown are too specific and should be generalized Unreachable blocks Contain statements that can never execute under any circumstances because the program logic “can’t get there” Microsoft Visual C# 2012, Fifth Edition

Using the finally Block Contains actions to perform at the end of a try…catch sequence Executes whether the try block identifies any Exceptions or not Used to perform clean-up tasks A finally block executes after: The try ends normally The catch executes The try ends abnormally and the catch does not execute Microsoft Visual C# 2012, Fifth Edition

Using the finally Block (cont’d.) Microsoft Visual C# 2012, Fifth Edition

Using the finally Block (cont’d.) Microsoft Visual C# 2012, Fifth Edition