EE4E. C++ Programming Lecture 6 Advanced Topics. Contents Introduction Introduction Exception handling in C++ Exception handling in C++  An object oriented.

Slides:



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

Exceptions Chapter Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Exception Handling Outline 13.1 Introduction 13.2 Exception-Handling Overview 13.3 Other.
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.
CS102--Object Oriented Programming
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.
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.
Exception Handling Handling an unexpected behaviour Unit - 08.
 2006 Pearson Education, Inc. All rights reserved. Exception Handling in C++ CS-2303, C-Term Exception Handling in C++ CS-2303 System Programming.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 23 - Exception Handling Outline 23.1Introduction 23.2When Exception Handling Should Be Used 23.3Other.
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.
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.
Exceptions and Exception Handling Carl Alphonce CSE116 March 9, 2007.
Exception Handling Yaodong Bi Exception Handling Java exception handling Try blocks Throwing and re-throwing an exception Catching an.
Exception Handling 1 CISC6795, Spring Introduction 2 Exception – an indication of a problem that occurs during a program’s execution, for examples:
Dale Roberts Exception Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Exception Handling: A Deeper.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
C++ Exception Handling
1 CSC241: Object Oriented Programming Lecture No 28.
Rossella Lau Lecture 9, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 9: Application with Exception Handling 
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
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.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Dale Roberts Exception Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
June 14, 2001Exception Handling in Java1 Richard S. Huntrods June 14, 2001 University of Calgary.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 13 - Exception Handling Outline 13.1Introduction 13.2When Exception Handling Should Be Used.
C++ Exceptions STL Vector. Example int Quotient (int numer, int denom} { if (denom != 0) return (numer/denom); else //What to do?? }
Exception Handling in Java Exception Handling Introduction: After completing this chapter, you will be able to comprehend the nature and kinds.
1 CSC241: Object Oriented Programming Lecture No 27.
COMPUTER PROGRAMMING 2 Exceptions. What are Exceptions? Unexpected events that happen when the code is executing (during runtime). Exceptions are types.
Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  Due to poor understanding of the problem and solution.
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.
EE4E. C++ Programming Lecture 6 Advanced Topics. Contents Introduction Introduction Exception handling in C++ Exception handling in C++  An object oriented.
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
Exceptions and Assertions Chapter 15 – CSCI 1302.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
Exceptions and Program Correctness based on the original work by Dr. Roger deBry Version 1.1.
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.
Exception Handling Outline 23.1 Introduction
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
Chapter 8-Exception Handling/ Robust Programming.
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.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 Object-Oriented Programming in C++ Exception.
Eighth Lecture Exception Handling in Java
Exception Handling in C++
IS 0020 Program Design and Software Tools
Exceptions Error Handling and Recovery
Chapter 16 Exception Handling: A Deeper Look
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.
CS212: Object Oriented Analysis and Design
CMSC202 Computer Science II for Majors Lecture 16 – Exceptions
Why exception handling in C++?
Chapter 14: Exception Handling
Exceptions 1 CMSC 202.
Object-Oriented Programming (OOP) Lecture No. 44
Department of Computer and Information Science, School of Science, IUPUI Exception Handling Dale Roberts, Lecturer Computer Science, IUPUI
CMSC 202 Lesson 20 Exceptions 1.
Presentation transcript:

EE4E. C++ Programming Lecture 6 Advanced Topics

Contents Introduction Introduction Exception handling in C++ Exception handling in C++  An object oriented approach to exception handling  try-catch model of exception handling  Exception handling example  Re-throwing exceptions  Exception specifications Multi-threading Multi-threading  Thread creation  Thread priority  Thread synchronisation

Introduction We will look at 2 more advanced topics in C++ We will look at 2 more advanced topics in C++  Exception handling  Multi-threading

An object oriented approach to exception handling Robust C++ programs must include exception handling Robust C++ programs must include exception handling Exceptions are error conditions encountered in executing class methods Exceptions are error conditions encountered in executing class methods  Attempting to read past an end of file  Attempting to read a file that doesn’t exist  Trying to open a malformed URL  Divide by zero  Taking the square root of a negative number  etc

Normal error handling (in C) would return an error code (eg. –1) Normal error handling (in C) would return an error code (eg. –1) class myClass { public: int readFile(….) { do { if (!end_of_file) // read the file else return –1; } while not_end_of_file return number_of_bytes_read; } };

This is a simple sometimes effective method but: This is a simple sometimes effective method but:  Sometimes not possible to return a valid error code.  Not object oriented! No information about the error is contained in the error code  Application code gets ‘polluted’ with error checking code  It would be nice to have all of the error handling in one place  The method might not be able to return normally from the error.  An example would be if a resource the method was accessing was not available

The try-catch model of exception handling Object-oriented applications comprise the use of pre-defined components (objects) by the application and the interaction between these objects Object-oriented applications comprise the use of pre-defined components (objects) by the application and the interaction between these objects  The object cannot know in advance how the application will process any error conditions it encounters  The try-catch mechanism is a means by which errors in object code can be communicated in a consistent way to the calling application

Application Object 1 Method call Error condition try clause catch clause

Exception handling example We will look at a simple example of exception handling involving handling a simple divide by zero error We will look at a simple example of exception handling involving handling a simple divide by zero error  The exception handler simply prints out an error message  However, the key point is that the program doesn’t terminate but allows the user to continue

#include class DivideByZeroException : public exception { public: DivideByZeroException::DivideByZeroException() : exception("Attempted divide by zero") {} }; double quotient(int num, int den) { if (den==0) throw DivideByZeroException(); return (double)(num)/den; }

int main() { int number1,number2; double result; cout<<"Enter two integers : "; while (cin >> number1 >> number2) { try { result=quotient(number1,number2); cout << "The quotient is " << result << endl; } catch(DivideByZeroException &dzException) { cout << "Exception! " << dzException.what() << endl; } cout << "\nEnter two integers : "; } return 0; }

Key point is the calling of quotient() within the try clause Key point is the calling of quotient() within the try clause  quotient() throws the DivideByZeroException exception using the throw() keyword if a zero denominator is input  This is then caught in the catch clause immediately after the try clause

Re-throwing exceptions Often, the exception handler is unable to adequately process the exception Often, the exception handler is unable to adequately process the exception  For example, it might not be appropriate for the exception to be handled within the object in which the exception was generated  Typically, the exception is then re-thrown on to an outer object  It is possible to have chains of unhandled exceptions re-thrown  At some level, the exception must be handled

main Object 2 Object 1 throws exception Calls method of re-throw handles exception re-throw

class MyClass1 { public: MyClass1() {} void aMethod1() { try { throw exception(); } catch (exception &caughtException) { cout << "Exception thrown in aMethod1" << endl; throw; } };

class MyClass2 { public: MyClass2() {} void aMethod2() { MyClass1 myObject1; try { myObject1.aMethod1(); } catch(exception &caughtException) { cout << "Exception re-thrown in aMethod2" << endl; throw; } };

int main() { MyClass2 myObject2; try { myObject2.aMethod2(); } catch(exception &caughtException) { cout << "Exception handled in main" << endl; } cout << “Program terminates "; return 0; }

In this simple example, an exception in generated in one object, re-thrown in the handler of the calling object and then handled in the main program In this simple example, an exception in generated in one object, re-thrown in the handler of the calling object and then handled in the main program  The program produces the following output: Exception thrown in aMethod1 Exception re-thrown in aMethod2 Exception handled in main Program terminates

Exception heirarchy C++ includes a hierarchy of exception classes headed by the base class exception C++ includes a hierarchy of exception classes headed by the base class exception  Includes a virtual method what() which derived classes can override to issue appropriate error messages  All exception classes defined in header  All exception classes defined in header

Exception specifications An exception specification (a throw list) enumerates a list of exceptions that a function can throw An exception specification (a throw list) enumerates a list of exceptions that a function can throw Indicates that the function throws exceptions only of types ExceptionA, ExceptionB, ExceptionC Indicates that the function throws exceptions only of types ExceptionA, ExceptionB, ExceptionC  If it throws a different type of exception, then function unexpected() is called which normally aborts the program int aFunction(int arg) throw (ExceptionA, ExceptionB, ExceptionC) { // function body)

(No exception specification.) Indicates function can throw any exception (No exception specification.) Indicates function can throw any exception (Empty specification.) Indicates the function cannot throw an exception (Empty specification.) Indicates the function cannot throw an exception  If it does, function unexpected() is called int aFunction(int arg) { // function body) int aFunction(int arg) throw() { // function body)

Comparison with Java Comparison with Java  Exception handling almost identical in Java and C++ with a few minor syntactic differences  The main difference is a member function must advertise when it throws an exception in Java  This function must then be called in a try-catch clause int aFunction(int arg) throws IOException // Java { // function body)

Stack unwinding This is a simple mechanism for handling uncaught exceptions This is a simple mechanism for handling uncaught exceptions  The function call stack is unwound to some catch clause in the function calling chain  Identical to simply re-throwing the exception

The following example is similar to a previous one – stack unwinding essentially causes the exception to be re-thrown to the calling object The following example is similar to a previous one – stack unwinding essentially causes the exception to be re-thrown to the calling object Only real difference is no explicit try-catch clauses Only real difference is no explicit try-catch clauses  This example throws a runtime_error exception  Standard C++ base class for representing run time errors

main Object 2 Calls method of stack unwind handles exception Object 1 throws exception

#include using std::runtime_error; class MyClass1 { public: MyClass1() {} void aMethod1() { throw runtime_error("runtime error in aMethod1"); } };

class MyClass2 { public: MyClass2() {} void aMethod2() { MyClass1 myObject1; myObject1.aMethod1(); } };

int main() { MyClass2 myObject2; try { myObject2.aMethod2(); } catch(runtime_error &error) { cout << "Exception occured: " << error.what() << endl; } return 0; }

Processing new failures When the object allocation operator new fails, it is important to handle this error When the object allocation operator new fails, it is important to handle this error  Otherwise it can lead to pointer access errors In older compilers, new returns 0 on failure In older compilers, new returns 0 on failure More recent compilers cause a new failure to return a bad_allocation exception More recent compilers cause a new failure to return a bad_allocation exception  bad_allocation defined in header file

‘Old-style’ handling of new failures: ‘Old-style’ handling of new failures: int main() { int* ptr = new int[500000]; if (ptr==0) { cout << “Memory allocation failure”; return 0; } else// Successful object allocation { // Use allocated memory } return 1; }

Exception handling approach to new failure Exception handling approach to new failure #include int main() { try { int* ptr = new int[500000]; } catch ( bad_alloc &memoryAllocException) { cout << “Memory allocation failure” << memoryAllocException.what(); } return 0; }

C++ allows even more flexibilty by allowing the programmer to define their own exception handler on new failure C++ allows even more flexibilty by allowing the programmer to define their own exception handler on new failure  set_new_handler() registers a function to be called on new failure  Allows the possibility of smart memory management  Useful for embedded systems where memory issues are critical

#include void myHandler() { cerr << “myHandler called”; abort(); } int main() { set_new_handler(myHandler) int* ptr = new int[500000]; cout << “Memory allocated”; return 0; }