 2006 Pearson Education, Inc. All rights reserved. 1 12 Exception Handling.

Slides:



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

 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Exception Handling Outline 13.1 Introduction 13.2 Exception-Handling Overview 13.3 Other.
Error Handling in.NET Exceptions. Error Handling Old way (Win32 API and COM): MyFunction() { error_1 = doSomething(); if (error_1) display error else.
Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer.
CSE 1302 Lecture 21 Exception Handling and Parallel Programming Richard Gesick.
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.
An Introduction to Java Programming and Object- Oriented Application Development Chapter 8 Exceptions and Assertions.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
Review Linked list: Doubly linked list, insertback, insertbefore Remove Search.
 2006 Pearson Education, Inc. All rights reserved. Exception Handling in C++ CS-2303, C-Term Exception Handling in C++ CS-2303 System Programming.
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.
 Both System.out and System.err are streams—a sequence of bytes.  System.out (the standard output stream) displays output  System.err (the standard.
Exception Handling Yaodong Bi Exception Handling Java exception handling Try blocks Throwing and re-throwing an exception Catching an.
 Pearson Education, Inc. All rights reserved Exception Handling.
Exception Handling 1 CISC6795, Spring Introduction 2 Exception – an indication of a problem that occurs during a program’s execution, for examples:
 2005 Pearson Education, Inc. All rights reserved Exception Handling.
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.
C++ Exception Handling
Exceptions Briana B. Morrison CSE 1302C Spring 2010.
Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling.
 2005 Pearson Education, Inc. All rights reserved Exception Handling.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
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.
Chapter 15 Strings String::Concat String::CompareTo, Equals, == If( string1 == S”Hello”) String1->Equals(S”Hello”) String1->CompareTo(S”Hello”) CompareTo.
Exceptions. 2 Objectives Introduce C# exception handling –library exception types –custom exceptions Describe keywords used for exception handling –try.
 2002 Prentice Hall. All rights reserved Exception-Handling Overview Exception handling –improves program clarity and modifiability by removing.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2009 Pearson Education, Inc. All rights reserved Exception Handling Many slides modified by Prof. L. Lilien (even many without explicit message).
 2006 Pearson Education, Inc. All rights reserved Exception Handling.
 2006 Pearson Education, Inc. All rights reserved Exception Handling.
 2009 Pearson Education, Inc. All rights reserved Exception Handling.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 9 : Exception Handling King Fahd University of Petroleum & Minerals College of Computer.
Dr. Abraham. Exception Any problem that VB or OS could not handle Robust program A program that performs well not only under ordinary conditions but also.
1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about.
CIS 270—Application Development II Chapter 13—Exception Handling.
Pemrograman VisualMinggu …12… Page 1 MINGGU Ke Duabelas Pemrograman Visual Pokok Bahasan: Exception Handling Tujuan Instruksional Khusus: Mahasiswa dapat.
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 –
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
Reformatted slides from the textbook, C++ How to Program, 6/e Pearson Education, Inc. All rights reserved Chapter 3. [Lecture 02] Introduction to.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 23 - Exception Handling Outline 23.1Introduction.
Exception Handling in C++. Outline What exceptions are and when to use them Using try, catch and throw to detect, handle and indicate exceptions, respectively.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Exceptions Exception handling.
C# Exceptions 1 CNS 3260 C#.NET Software Development.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Lecture 18B Exception Handling and Richard Gesick.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Enhanced Car Payment Calculator Application Introducing Exception Handling.
Appendix H Exception Handling: A Deeper Look
Chapter 11 – Exception Handling
Exception Handling in C++
16 Exception Handling.
13 Exception Handling.
Chapter 14: Exception Handling
Exception Handling and
CNS 3260 C# .NET Software Development
Exception Handling Chapter 9 Edited by JJ.
Part B – Structured Exception Handling
Fundaments of Game Design
16 Exception Handling.
Exception Handling.
Presentation transcript:

 2006 Pearson Education, Inc. All rights reserved Exception Handling

 2006 Pearson Education, Inc. All rights reserved. 2 It is common sense to take a method and try it. If it fails, admit it frankly and try another. But above all, try something. — Franklin Delano Roosevelt O! throw away the worser part of it, And live the purer with the other half. — William Shakespeare

 2006 Pearson Education, Inc. All rights reserved. 3 And oftentimes excusing of a fault Doth make the fault the worse by the excuse. — William Shakespeare If they’re running and they don’t look where they’re going I have to come out from somewhere and catch them. — J. D. Salinger O infinite virtue! com’st thou smiling from the world’s great snare uncaught? — William Shakespeare

 2006 Pearson Education, Inc. All rights reserved. 4 OBJECTIVES In this chapter you will learn:  What exceptions are and how they are handled.  When to use exception handling.  To use try blocks to delimit code in which exceptions might occur.  To throw exceptions to indicate a problem.  To use catch blocks to specify exception handlers.  To use the finally block to release resources.  The.NET exception class hierarchy.  Exception properties.  To create user-defined exceptions.

 2006 Pearson Education, Inc. All rights reserved Introduction 12.2 Exception Handling Overview 12.3 Example: Divide by Zero Without Exception Handling 12.4 Example: Handling DivideByZeroException s and FormatException s Enclosing Code in a try Block Catching Exceptions Uncaught Exceptions Termination Model of Exception Handling Flow of Control When Exceptions Occur 12.5.NET Exception Hierarchy Classes ApplicationException and SystemException Determining Which Exceptions a Method Throws 12.6 finally Block 12.7 Exception Properties 12.8 User-Defined Exception Classes 12.9 Wrap-Up

 2006 Pearson Education, Inc. All rights reserved. 6 Error-Prevention Tip 12.1 Exception handling helps improve a program’s fault tolerance.

 2006 Pearson Education, Inc. All rights reserved. 7 Outline DivideByZeroNoExce ptionHandling.cs (1 of 2)

 2006 Pearson Education, Inc. All rights reserved. 8 Outline DivideByZeroNoExce ptionHandling.cs (2 of 2)

 2006 Pearson Education, Inc. All rights reserved. 9 Outline DivideByZeroTest.cs (1 of 3)

 2006 Pearson Education, Inc. All rights reserved. 10 Outline DivideByZeroTest.cs (2 of 3)

 2006 Pearson Education, Inc. All rights reserved. 11 Outline DivideByZeroTest.cs (3 of 3) (a) (b) (d) (c) (e)

 2006 Pearson Education, Inc. All rights reserved. 12 Fig | Exception Assistant. Throw point Exception Assistant

 2006 Pearson Education, Inc. All rights reserved. 13 Common Programming Error 12.1 Logic errors can occur if you assume that after an exception is handled, control will return to the first statement after the throw point.

 2006 Pearson Education, Inc. All rights reserved. 14 Common Programming Error 12.2 Specifying a comma-separated list of parameters in a catch block is a syntax error. A catch block can have at most one parameter.

 2006 Pearson Education, Inc. All rights reserved. 15 Common Programming Error 12.3 It is a compilation error if a catch block that catches a base-class exception is placed before a catch block for any of that class’s derived-class types. If this were allowed, the base-class catch block would catch all base-class and derived-class exceptions, so the derived-class exception handler would never execute.

 2006 Pearson Education, Inc. All rights reserved. 16 Software Engineering Observation 12.1 If a method throws exceptions, statements that invoke the method directly or indirectly should be placed in try blocks, and those exceptions should be caught and handled.

 2006 Pearson Education, Inc. All rights reserved. 17 Error-Prevention Tip 12.2 The CLR does not completely eliminate memory leaks. The CLR will not garbage collect an object until the program contains no more references to that object. Thus, memory leaks can occur if programmers inadvertently keep references to unwanted objects.

 2006 Pearson Education, Inc. All rights reserved. 18 Error-Prevention Tip 12.3 A finally block typically contains code to release resources acquired in the corresponding try block, which makes the finally block an effective mechanism for eliminating resource leaks.

 2006 Pearson Education, Inc. All rights reserved. 19 Performance Tip 12.1 As a rule, resources should be released as soon as they are no longer needed in a program. This makes them available for reuse promptly.

 2006 Pearson Education, Inc. All rights reserved. 20 Common Programming Error 12.4 Placing the finally block before a catch block is a syntax error.

 2006 Pearson Education, Inc. All rights reserved. 21 Outline UsingExceptions.cs (1 of 8)

 2006 Pearson Education, Inc. All rights reserved. 22 Outline UsingExceptions.cs (2 of 8)

 2006 Pearson Education, Inc. All rights reserved. 23 Outline UsingExceptions.cs (3 of 8)

 2006 Pearson Education, Inc. All rights reserved. 24 Outline UsingExceptions.cs (4 of 8)

 2006 Pearson Education, Inc. All rights reserved. 25 Outline UsingExceptions.cs (5 of 8)

 2006 Pearson Education, Inc. All rights reserved. 26 Outline UsingExceptions.cs (6 of 8)

 2006 Pearson Education, Inc. All rights reserved. 27 Outline UsingExceptions.cs (7 of 8)

 2006 Pearson Education, Inc. All rights reserved. 28 Outline UsingExceptions.cs (8 of 8)

 2006 Pearson Education, Inc. All rights reserved. 29 Common Programming Error 12.5 It is a compilation error if the argument of a throw —an exception object—is not of class Exception or one of its derived classes.

 2006 Pearson Education, Inc. All rights reserved. 30 Common Programming Error 12.6 Throwing an exception from a finally block can be dangerous. If an uncaught exception is awaiting processing when the finally block executes, and the finally block throws a new exception that is not caught in the finally block, the first exception is lost, and the new exception is passed to the next enclosing try block.

 2006 Pearson Education, Inc. All rights reserved. 31 Error-Prevention Tip 12.4 When placing code that can throw an exception in a finally block, always enclose the code in a try statement that catches the appropriate exception types. This prevents the loss of any uncaught and rethrown exceptions that occur before the finally block executes.

 2006 Pearson Education, Inc. All rights reserved. 32 Software Engineering Observation 12.2 Do not place try blocks around every statement that might throw an exception, because this can make programs difficult to read. It is better to place one try block around a significant portion of code, and follow this try block with catch blocks that handle each of the possible exceptions. Then follow the catch blocks with a single finally block. Separate try blocks should be used when it is important to distinguish between multiple statements that can throw the same exception type.

 2006 Pearson Education, Inc. All rights reserved. 33 Error-Prevention Tip 12.5 A stack trace shows the complete method-call stack at the time an exception occurred. This enables the programmer to view the series of method calls that led to the exception. Information in the stack trace includes the names of the methods on the call stack at the time of the exception, the names of the classes in which the methods are defined and the names of the namespaces in which the classes are defined. If the program database (PDB) file that contains the debugging information for the method is available, the stack trace also includes line numbers; the first line number indicates the throw point, and subsequent line numbers indicate the locations from which the methods in the stack trace were called. PDB files are created by the IDE to maintain the debugging information for your projects.

 2006 Pearson Education, Inc. All rights reserved. 34 Outline Properties.cs (1 of 4)

 2006 Pearson Education, Inc. All rights reserved. 35 Outline Properties.cs (2 of 4)

 2006 Pearson Education, Inc. All rights reserved. 36 Outline Properties.cs (3 of 4)

 2006 Pearson Education, Inc. All rights reserved. 37 Outline Properties.cs (4 of 4)

 2006 Pearson Education, Inc. All rights reserved. 38 Error-Prevention Tip 12.6 When catching and rethrowing an exception, provide additional debugging information in the rethrown exception. To do so, create an Exception object containing more specific debugging information, then pass the original caught exception to the new exception object’s constructor to initialize the InnerException property.

 2006 Pearson Education, Inc. All rights reserved. 39 Good Programming Practice 12.1 Associating each type of malfunction with an appropriately named exception class improves program clarity.

 2006 Pearson Education, Inc. All rights reserved. 40 Software Engineering Observation 12.3 Before creating a user-defined exception class, investigate the existing exceptions in the.NET Framework Class Library to determine whether an appropriate exception type already exists.

 2006 Pearson Education, Inc. All rights reserved. 41 Outline NegativeNumberExce ption.cs (1 of 2)

 2006 Pearson Education, Inc. All rights reserved. 42 Outline NegativeNumberExce ption.cs (2 of 2)

 2006 Pearson Education, Inc. All rights reserved. 43 Outline SquareRootTest.cs (1 of 3)

 2006 Pearson Education, Inc. All rights reserved. 44 Outline SquareRootTest.cs (2 of 3)

 2006 Pearson Education, Inc. All rights reserved. 45 Outline SquareRootTest.cs (3 of 3) (a) (c) (b) (d)