CMSC202 Computer Science II for Majors Lecture 16 – Exceptions

Slides:



Advertisements
Similar presentations
CS102--Object Oriented Programming
Advertisements

C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
CSE 332: C++ exceptions Overview of C++ Exceptions Normal program control flow is halted –At the point where an exception is thrown The program call stack.
Lesson 16 Exceptions Lesson Exceptions1. Murphy’s Law Anything that can go wrong will go wrong Lesson Exceptions2.
Exceptions Briana B. Morrison CSE 1302C Spring 2010.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
Exception Handling Introduction Exception handling is a mechanism to handle exceptions. Exceptions are error like situations. It is difficult to decide.
CS Advanced C++ Exception Handling Topic #5.
Exceptions Objectives At the conclusion of this lesson, students should be able to Explain the need for exceptions Correctly write programs that use.
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
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
1 CSC241: Object Oriented Programming Lecture No 27.
Exceptions Handling Exceptionally Sticky Problems.
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
Exception Handling Programmers must deal with errors and exceptional situations: User input errors Device errors Empty disk space, no memory Component.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling.
CSCI 383 Object-Oriented Programming & Design Lecture 20 Martin van Bommel.
LECTURE LECTURE 14 Exception Handling Textbook p
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Exceptions Exception handling.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Exception Handling How to handle the runtime errors.
EE4E. C++ Programming Lecture 6 Advanced Topics. Contents Introduction Introduction Exception handling in C++ Exception handling in C++  An object oriented.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
C ++ MULTIPLE CHOICE QUESTION
Exception handling.
Exception Handling in C++
C++ Exceptions.
IS 0020 Program Design and Software Tools
Exceptions Error Handling and Recovery
Chapter 16 Exception Handling
Abstract Class Abstract Class is a class which contains atleast one Pure Virtual function in it. Abstract classes are used to provide an Interface for.
Exceptions Exceptions are used to signal that an unexpected event has happened in a program C++ will generate exceptions for some errors in the program.
Exceptions.
Exceptions In this lecture:
Handling Exceptionally Sticky Problems
Object-Oriented Programming (OOP) Lecture No. 45
Why exception handling in C++?
CMSC202 Computer Science II for Majors Lecture 08 – Overloaded Constructors Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
Part IX Fundamentals of C and C++ Programming Exception Handling
EXCEPTION HANDLING.
CMSC 202 Lesson 21 Exceptions II.
Exceptions C++ Interlude 3
Chapter 14: Exception Handling
Exception Handling Chapter 9.
Exceptions with Functions
Chapter 12 Exception Handling and Text IO
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 Chapter 9 Edited by JJ.
Exceptions 2 CMSC 202.
Throwing and catching exceptions
Advanced C++ Exception Handling
Exceptions and Templates
Exceptions CSCE 121 J. Michael Moore
Exception Handling.
Exceptions 1 CMSC 202.
CISC/CMPE320 - Prof. McLeod
Exception Handling Imran Rashid CTO at ManiWeber Technologies.
Standard Version of Starting Out with C++, 4th Edition
Exceptions, Templates, and the Standard Template Library (STL)
CMSC 202 Exceptions 2nd Lecture.
Handling Exceptionally Sticky Problems
Object-Oriented Programming (OOP) Lecture No. 44
CMSC 202 Exceptions 2nd Lecture.
CMSC 202 Exceptions.
Exception Handling.
CMSC 202 Lesson 20 Exceptions 1.
Presentation transcript:

CMSC202 Computer Science II for Majors Lecture 16 – Exceptions Dr. Katherine Gibson

Last Class We Covered Inheritance Polymorphism Virtual functions Abstract Classes Exam 2

Any Questions from Last Time?

Today’s Objectives Error handling Exceptions Defining exception classes Using exceptions Try Throw Catch When to throw exceptions

Error Handling

Common Errors We have seen a number of error types: Could not allocate memory Out-of-bounds on vector File not found/could not be opened Attempting to add a train car that’s not allowed A poker hand with invalid cards

Handling Errors – Now How are these errors handled? Print a message “You cannot add a second Snack Car” Do nothing Exit the program The errors are handled right where they occur

Handling Errors at Occurence Advantages: Easy to find because code is right there Disadvantages: Error handling scattered throughout code Code duplication Code inconsistency (even worse!) Errors are handled however the original coder decided would be best

Two “Coders” for Each Class Class implementer Creates the class definition Knows what constitutes an error Decides how to handle errors Class user Uses the class implementation Knows how they want to handle errors (But if handled internally, the class user may not even know an error occurred)

Separating Errors Want to separate errors into two pieces: Error detection Implementer knows how to detect Error handling User can decide how to handle Use exceptions to do this

Exceptions

Exceptional Cases Exceptions are used to handle exceptional cases Cases that shouldn’t occur normally Allow us to indicate an error has occurred without explicitly handling it C++ uses these too, like when we try to use .at() to examine an out-of-bounds element

Try / Throw / Catch Exceptions are implemented using the keywords try, throw, and catch

Try / Throw / Catch Exceptions are implemented using the keywords try, throw, and catch The try keyword means we are going to try something, even though we are not sure it is going to perform correctly

Try / Throw / Catch Exceptions are implemented using the keywords try, throw, and catch The throw keyword is used when we encounter an error Means we are going to “throw” two things A value (explicit) Control flow (implicit)

Try / Throw / Catch Exceptions are implemented using the keywords try, throw, and catch The catch keyword means we are going to try to catch at most one type of value To catch different types of values, we need multiple catch statements

Exception Example // inside SetCarID() function if (newID < MIN_ID_VAL || newID > MAX_ID_VAL) { cerr << "ID invalid, no change"; }

Exception Example // inside SetCarID() function try { if (newID < MIN_ID_VAL || newID > MAX_ID_VAL) { cerr << "ID invalid, no change"; } catch () {

Exception Example // inside SetCarID() function try { if (newID < MIN_ID_VAL || newID > MAX_ID_VAL) { throw(newID); } catch () {

Exception Example // inside SetCarID() function try { if (newID < MIN_ID_VAL || newID > MAX_ID_VAL) { throw(newID); } catch (int ID) {

Exception Example // inside SetCarID() function try { if (newID < MIN_ID_VAL || newID > MAX_ID_VAL) { throw(newID); } catch (int ID) { cerr << "ID invalid, no change";

Catching and Throwing

Using Catch The catch keyword requires: One parameter Typename (int, exception, out_of_range, etc) Name (newID, e, oor, etc.) [optional] To catch multiple types of exceptions, you need to use multiple catch blocks

Using Catch You can throw from inside a catch block But this should be done sparingly and only after careful consideration Most of the time, a nested try-catch means you should re-evaluate your program design Uncaught exceptions will cause the terminate() function to be called

Using Catch Catch blocks are run in order, so exceptions should be caught in order from Most specific to least specific To catch all possible exceptions, use: catch(...) (Literally use three periods as a parameter)

Throwing Out of a Function We can throw exceptions without try/catch Most commonly done within functions Requires that we list possible exception types in the function prototype and definition Called a throw list

Throw Lists Warn programmers that functions throw exceptions without catching them Throw lists should match up with what is thrown and not caught inside the function Otherwise, it can lead to a variety of errors, including the function unexpected() Can also have empty throw lists for clarity: int GetCarID() throw ();

Throw List Syntax Functions can specify their throw lists // Throws only 1 type of exception retType funcName( params ) throw (excep); // Throws 2 types of exceptions (comma separated list) retType funcName( params ) throw (excep1, excep2); // Promises not to throw any exceptions retType funcName( params ) throw ( ); // Can throw any exceptions [backwards compatibility] retType funcName( params );

Throw List Example: Inside void SetCarID(int newID) throw (int) { if (newID < MIN_ID_VAL || newID > MAX_ID_VAL) { throw(newID); } else { m_carID = newID; this function might throw an integer

Throw List Example: Outside v0 // inside main() train.at(0).SetCarID(-1); What will happen if we run this code? The exception won’t be caught The terminate() function will be called

Throw List Example: Outside v1 // inside main() try { train.at(0).SetCarID(-1); } catch (int ID) { cerr << "ID invalid, no change"; } this user has based their code on getting input from a file

Throw List Example: Outside v2 // inside main() while(set == false) { try { train.at(0).SetCarID(userID); set = true; } catch (int ID) { cerr << "ID" << ID << "invalid, give another"; cin >> userID; } this user has based their code on getting input from a user, and being able to repeat requests

Exception Classes

Exception Classes We can create, throw, and catch exception classes that we have created We can even create hierarchies of exception classes using inheritance Catching the parent class will also catch all child class exceptions

Exception Class Example class MathError { /*...*/ }; class DivideByZeroError: public MathError { /*...*/ }; class InvalidNegativeError: public MathError { /*...*/ };

Creating Exception Classes Name of class reflects the error Not the code that throws error Contains basic information or a message Parameter value Name of function that detected error Description of error Methods required Constructor (one or more) Accessor (one or more)

Nested Functions? What happens here? // function2 throws an exception void function2( ) { cout << "function2" << endl; throw int(42); } // function1 calls function2, // but with no try/catch void function1( ) function2( ); cout << "function1" << endl; // main calls function1, // with try/catch int main( ) { try { function1( ); } catch (int) cout << "Exception" << "occurred" << endl; return 0; What happens here? Stack is unwound until something catches the exception OR until unwinding passes main What happens then?

Exceptions in Constructors Best way to handle Constructor failure Replaces Zombie objects! Any sub-objects that were successfully created are destroyed (destructor is not called!) Example: // MyClass constructor MyClass::MyClass ( int value ) { m_pValue = new int(value); // pretend something bad happened throw NotConstructed( ); }

Exceptions in Destructors Bad, bad idea… What if your object is being destroyed in response to another exception? Should runtime start handling your exception or the previous one? General Rule… Do not throw exceptions in destructor

Announcements Project 4 is out! We’ll go over Exam 2 next time