2.4 Exceptions n Detects try { //code that may raise an exception and/or set some condition if (condition) throw exceptionName; //Freq. A string } n Handles.

Slides:



Advertisements
Similar presentations
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Exception Handling Outline 13.1 Introduction 13.2 Exception-Handling Overview 13.3 Other.
Advertisements

Lecture 9. 2 Exception  An exception is a unusual, often unpredictable event, detectable by software or hardware, that requires special processing occurring.
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
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.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
1 CSC241: Object Oriented Programming Lecture No 28.
Exception Handling.  What are errors?  What does exception handling allow us to do?  Where are exceptions handled?  What does exception handling facilitate?
CS 206 Introduction to Computer Science II 09 / 09 / 2009 Instructor: Michael Eckmann.
Software Testing and Quality Assurance
Subclasses and Subtypes CMPS Subclasses and Subtypes A class is a subclass if it has been built using inheritance. ▫ It says nothing about the meaning.
CPSC 252 Exception Handling Page 1 Exceptions and exception handling Client programmers can make errors using a class attempting to dequeue an item from.
Object Oriented Programming
C++ Exceptions STL Vector. Example int Quotient (int numer, int denom} { if (denom != 0) return (numer/denom); else //What to do?? }
COMPUTER PROGRAMMING 2 Exceptions. What are Exceptions? Unexpected events that happen when the code is executing (during runtime). Exceptions are types.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Contract based programming Using pre- and post-conditions, and object invariants Contract based programming1.
Exceptions Handling Exceptionally Sticky Problems.
6. Testing and Verification
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 7 Files.
Opening Input/Output Files void openFiles ( ifstream& infile, ofstream& outfile) { char inFileName[40]; char outFileName[40]; cout
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Computer Science 209 Software Development Handing Errors and Creating Documentation.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
1 Today’s Objectives  Announcements Homework #5 is due today. Since this date is so close to the end of the semester, no late assignments will be accepted.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Manipulator example #include int main (void) { double x = ; streamsize prec = cout.precision(); cout
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Exceptions Exception handling.
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 Object-Oriented Programming in C++ Exception.
Today… StringTokenizer class. Method Overloading. Catching Exceptions (and what they are!). Start Pointers and Aliasing. Winter 2016CMPE212 - Prof. McLeod1.
C ++ MULTIPLE CHOICE QUESTION
Exception handling.
IS 0020 Program Design and Software Tools
Exceptions Error Handling and Recovery
CMSC202 Computer Science II for Majors Lecture 16 – Exceptions
Andy Wang Object Oriented Programming in C++ COP 3330
Abstract Data Type.
Handling Exceptionally Sticky Problems
Why exception handling in C++?
Specifications What? Not how!.
Object Oriented Programming COP3330 / CGS5409
Exceptions C++ Interlude 3
Chapter 14: Exception Handling
Exception Handling and
Chapter 11—Exceptions Handling Exceptions Throwing Exceptions.
Advanced Java Programming
Topics Introduction to File Input and Output
Chapter 7 Files and Exceptions
Exceptions with Functions
Exceptions Problems in a Java program may cause exceptions or errors representing unusual or invalid processing. An exception is an object that defines.
CSC 143 Error Handling Kinds of errors: invalid input vs programming bugs How to handle: Bugs: use assert to trap during testing Bad data: should never.
Exception Handling Chapter 9 Edited by JJ.
Data Abstraction: The Walls
Andy Wang Object Oriented Programming in C++ COP 3330
Exception Handling Oo28.
Part B – Structured Exception Handling
CSC 270 – Survey of Programming Languages
Exceptions 1 CMSC 202.
Handling Exceptionally Sticky Problems
Topics Introduction to File Input and Output
Lecture 9.
CMSC 202 Exceptions.
Basic Exception Handling
CIS 110: Introduction to Computer Programming
Presentation transcript:

2.4 Exceptions n Detects try { //code that may raise an exception and/or set some condition if (condition) throw exceptionName; //Freq. A string } n Handles Catch (exceptionType variable) { //code handles } //code to continue processing //executed unless catch block stops processing with halt; or return

Example … try { infile >> value; if (value < 0 ) throw string (“Negative value”); y = sqrt (value); } catch (string message ) { //code handles cout << message << “found in file. Abort.”; return –1 } //code to continue if exception not thrown cout << “sqrt of “ << value << “is “ << y << endl;

Types to throw and catch n strings – for messages n ints – to use in a case to handle different reasons exception thrown

Specifying Functions n Up to now Purpose/Receives/Returns n Text Book uses Pre- and Post-conditions n Pre-condition: describes what must be true about the state for the function to work correctly n Post-condition: describes what will be true about the state if the pre-conditions are met

Specifying Functions cont. n Precondition – discusses appropriate values for receives parameters and member data of object if it is a member function we are specifying n Post-condition – describes what the return variables values will be and what changes will be made to the object

Error Conditions Describe what preconditions will be checked by the function (might use try/catch)

Primary vs Enhanced Operations n Primary -Member functions – Constructor – Transformers n Change 1 piece of ADT n Add 1 piece to ADT n Delete 1 piece of ADT – Observers n Accessors – look at 1 piece of ADT n Predicates – return a bool value regarding state of ADT – Destructor n Enhanced - Non-member functions – manipulate many pieces of ADT (iterators) – manipulate several pieces of ADT – Manipulate several instances of same type of ADT – Call primary operations to get work done

#ifndef n #ifndef FILE_H n #define FILE_H n #endif