Exception Handling Handling an unexpected behaviour Unit - 08.

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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Lecture 9. 2 Exception  An exception is a unusual, often unpredictable event, detectable by software or hardware, that requires special processing occurring.
Exception Handling Introduction Try Throw Catch. Exception Handling Use exception handling to write programs that are –Clearer –More robust –More fault-tolerant.
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.
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.
Exceptions, Templates, And The Standard Template Library (STL) Chapter 16.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
 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.
CSIS 123A Lecture 11 Exception Handling. Introduction  Typical approach to development:  Write programs assuming things go as planned  Get ‘core’ working.
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.
Exception Handling Yaodong Bi Exception Handling Java exception handling Try blocks Throwing and re-throwing an exception Catching an.
Dale Roberts Exception Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
Lesson 16 Exceptions Lesson Exceptions1. Murphy’s Law Anything that can go wrong will go wrong Lesson Exceptions2.
C++ Exception Handling
Exception Handling Introduction Exception handling is a mechanism to handle exceptions. Exceptions are error like situations. It is difficult to decide.
Exceptions COMPSCI 105 S Principles of Computer Science.
Dale Roberts Exception Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
Object Oriented Programming
1 CSC241: Object Oriented Programming Lecture No 27.
Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  Due to poor understanding of the problem and solution.
Exception Handling in JAVA. Introduction Exception is an abnormal condition that arises when executing a program. In the languages that do not support.
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.
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
Exception Handling Programmers must deal with errors and exceptional situations: User input errors Device errors Empty disk space, no memory Component.
CS212: Object Oriented Analysis and Design Lecture 20: Exception Handling-II.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
(13-1) Exception Handling in C++ D & D Chapter 17 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Unit 4 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
11. EXCEPTION HANDLING Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
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.
Copyright © Curt Hill Error Handling in Java Throwing and catching exceptions.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Exceptions Exception handling.
Exception Handling How to handle the runtime errors.
CSC241 Object-Oriented Programming (OOP) Lecture No. 22.
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.
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 Object-Oriented Programming in C++ Exception.
Namespace יום ראשון 13 נובמבר 2016 יום ראשון 13 נובמבר 2016 יום ראשון 13 נובמבר 2016 יום ראשון 13 נובמבר 2016 יום ראשון 13 נובמבר 2016 יום ראשון 13 נובמבר.
Exception Handling in C++
IS 0020 Program Design and Software Tools
Exceptions Error Handling and Recovery
16 Exception Handling.
Jim Fawcett CSE687-OnLine – Object Oriented Design Summer 2017
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
Jim Fawcett CSE687 – Object Oriented Design Spring 2001
Jim Fawcett CSE687 – Object Oriented Design Spring 2015
Why exception handling in C++?
Part IX Fundamentals of C and C++ Programming Exception Handling
Throwing and catching exceptions
Exceptions 1 CMSC 202.
Exception Handling Imran Rashid CTO at ManiWeber Technologies.
Object-Oriented Programming (OOP) Lecture No. 43
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
Exception Handling.
CMSC 202 Lesson 20 Exceptions 1.
Presentation transcript:

Exception Handling Handling an unexpected behaviour Unit - 08

Unit Introduction This unit covers C++ exception handling mechanism

Unit Objectives After covering this unit you will understand… Exception handling Exception handling Throwing exceptions Throwing exceptions Catching an exception Catching an exception Termination vs. Resumption Termination vs. Resumption Exception specification Exception specification Uncaught exception Uncaught exception Exception objects Exception objects

Exception Handling To recover from undesirable situation To recover from undesirable situation To increase robustness of code To increase robustness of code Deal with synchronous errors Deal with synchronous errors Recovery procedure as exception handler Recovery procedure as exception handler

Throwing an Exception Throwing an exception as raising an exception Throwing an exception as raising an exception We can specify the exceptions a function throws We can specify the exceptions a function throws Point at which throw is executed, is called throw point Point at which throw is executed, is called throw point Use of throw keyword Use of throw keyword throw MyException(“This is My Exception”);

Re-throwing an Exception In catch block, an exception caught can be re- thrown to a higher level using throw In catch block, an exception caught can be re- thrown to a higher level using throw throw statement should appear in a catch handler otherwise it will cause a call to terminate throw statement should appear in a catch handler otherwise it will cause a call to terminate catch(...) // catching any general exception { cout << ”An Exception occurred”; cout << ”An Exception occurred”; throw; // re-throwing an exception throw; // re-throwing an exception}

Catching an Exception Use of try and catch keyword Use of try and catch keyword To solve the problem within same scope To solve the problem within same scope catch blocks, as exception handlers catch blocks, as exception handlers Only one exception handler for every exception type Only one exception handler for every exception type try {// code that may generate exceptions } catch(ExceptionType1 id1) // specific type { // to handle exceptions of type1 } catch(...) // general exception { // to handle any general exception }

Catching an Exception (contd.) Enclose the code in try block that can throw an exception Enclose the code in try block that can throw an exception For a try block place one or more catch blocks For a try block place one or more catch blocks In each catch block catch the required exception that need to be caught In each catch block catch the required exception that need to be caught Catching (…) means any exception will be caught Catching (…) means any exception will be caught

Termination vs. Resumption Two models in exception handling theory Two models in exception handling theory Unrecoverable error cause termination Unrecoverable error cause termination To resume program execution after recovering form error is resumption To resume program execution after recovering form error is resumption

Example: Exception Handling class BaseException { public: public: BaseException() { m_pError = “Base Exception”; } void PrintError() const { cout << m_pError; } void PrintError() const { cout << m_pError; } char* m_pError; }; class DerivedException : public BaseException { public: public: DerivedException() {m_pError = “Derived Exception”; } }; float quotient(int num1, int num2) { if(num1 == 0 && num2 == 0) if(num1 == 0 && num2 == 0) throw BaseException(); throw BaseException(); else if(num2 == 0) else if(num2 == 0) throw DerivedException();

Example: Exception Handling (contd.) return (float) num1 / num2; // normal flow of execution } void main() { try try { { float result = quotient(5, 0); float result = quotient(5, 0); cout << “Quotient is:” << result; cout << “Quotient is:” << result; throw 1; //throwing primitive type exception throw 1; //throwing primitive type exception} catch(DerivedException exception) { exception.printError(); exception.printError();}

Example: Exception Handling (contd.) catch(BaseException exception) { exception.printError(); exception.printError(); } catch(...) { cout << ”General exception”; cout << ”General exception”; throw; // re-throwing an exception throw; // re-throwing an exception } } catch(...) catch(...) { cout << “General exception again occurred”; }}

Exception Specification Enables a list of exceptions that can be thrown by a function to be specified Enables a list of exceptions that can be thrown by a function to be specified Exception specification is also called throw list Exception specification is also called throw list throw clause is used in function signature throw clause is used in function signature void Function() throw(DivByZero, IllegalNumber); void Function() throw(DivByZero, IllegalNumber); void Function(); void Function(); void Function() throw; void Function() throw;

Exception Specification (contd.) unexpected() is called if an exception is thrown that is not in exception specification which by default calls terminate() unexpected() is called if an exception is thrown that is not in exception specification which by default calls terminate() A user function can be specified when unexpected() is called by set_unexpected function A user function can be specified when unexpected() is called by set_unexpected function abort() will automatically be called after the last action of user-defined termination function abort() will automatically be called after the last action of user-defined termination function

Example: Exception Specification class Exception1 {}; class Exception2 {}; void UnexpectedException(); void Function(int i) throw (Exception1, Exception2) { switch(i) switch(i) { case 1:throw Exception1(); case 2:throw Exception2(); } UnexpectedException(); // calls unexpected function UnexpectedException(); // calls unexpected function} void UnexpectedException() { throw 1; } void MyTerminationFunction() {cout << “unexpected exception occurred”; }

Example: Exception Specification (contd.) void main() { set_unexpected(MyTerminationFunction); set_unexpected(MyTerminationFunction); // ignores return value // ignores return value for(int i = 1; i <=3; i++) for(int i = 1; i <=3; i++) try try {function(i); } catch(Exception1) catch(Exception1) { cout << "Exception1 occurred"; } catch(Exception2) catch(Exception2) { cout << "Exception2 occurred"; }}

Uncaught Exception If an exception occurs that has no matching catch block, terminate() is called If an exception occurs that has no matching catch block, terminate() is called terminate() is a pointer to function, by default this function is abort() terminate() is a pointer to function, by default this function is abort() set_terminate() can be used to set a user defined function, when terminate() will be called set_terminate() can be used to set a user defined function, when terminate() will be called

Example: Uncaught Exception void MyTerminate() { cout << "My terminate"; cout << "My terminate"; abort(); // must be aborted abort(); // must be aborted} void (*ptr_fn)()= set_terminate(MyTerminate); //ptr to fn class Outer { public: public: class Inner {}; void Function() { cout << "Outer::function()"; cout << "Outer::function()"; throw Inner(); throw Inner();}};

Example: Uncaught Exception (contd.) void main() { try try { Outer obj; obj.Function(); // results in call to terminate // function // function } catch(Outer) catch(Outer) { cout << "trying to catch outer exception"; } // no handler for Inner type exception // no handler for Inner type exception}

Exception Objects Exception to be caught can be as simple as an int and as complex as a complete class type Exception to be caught can be as simple as an int and as complex as a complete class type Exception object should be caught by reference, and not by value Exception object should be caught by reference, and not by value Exception can also be caught as a pointer Exception can also be caught as a pointer

Example: Catch By Reference class Parent { public: public: virtual void Print() { cout << "Parent"; cout << "Parent";}}; class Child : public Parent { public: public: void Print() { cout << "Child"; } }; void Function() { throw Child(); } // function throwing exception of type Child

Example: Catch By Reference (contd.) int main() { try try {Function(); } catch(Parent p) // catch by value, child object sliced catch(Parent p) // catch by value, child object sliced {p.Print(); } try try {Function(); } catch(Parent& p) //catch by reference catch(Parent& p) //catch by reference {p.Print(); }}

Unit Summary In this unit you have covered … In this unit you have covered … Handling of exceptions Handling of exceptions Throwing exceptions Throwing exceptions Catching an exception Catching an exception Exception specification Exception specification Uncaught exception Uncaught exception Exception objects Exception objects