© Copyright Eliyahu Brutman Exceptions. © Copyright Eliyahu Brutman Exceptions and Design Patterns - 2 Introduction to Exception Handling Definition:

Slides:



Advertisements
Similar presentations
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.
Advertisements

1 Chapter 17-2 Templates and Exceptions Dale/Weems.
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.
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.
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.
1 Lecture 8 OOP Course. 9. Exceptions Error handling in C++ Error handling in C++
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.
Exception Handling Introduction Exception handling is a mechanism to handle exceptions. Exceptions are error like situations. It is difficult to decide.
CSI 3120, Exception handling, page 1 Exception and Event Handling Credits Robert W. Sebesta, Concepts of Programming Languages, 8 th ed., 2007 Dr. Nathalie.
CS Advanced C++ Exception Handling Topic #5.
Chapter 11 Exception Handling and Event Handling.
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
OOP Spring 2007 – Recitation 81 Object Oriented Programming Spring 2007 Recitation 8.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
Exceptions Objectives At the conclusion of this lesson, students should be able to Explain the need for exceptions Correctly write programs that use.
Chapter 11: Handling Exceptions and Events J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Fourth.
Exception Error handling. Exception 4 n An unusual occurrence during program execution that requires immediate handling n Errors are the most common type.
1 Exception and Event Handling (Based on:Concepts of Programming Languages, 8 th edition, by Robert W. Sebesta, 2007)
What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic.
Object Oriented Programming
1 CSC241: Object Oriented Programming Lecture No 27.
Slides Credit Umair Javed LUMS Web Application Development.
Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  Due to poor understanding of the problem and solution.
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.
Java Programming: Guided Learning with Early Objects
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.
Data Structures Using Java1 Chapter 2 Inheritance and Exception Handling.
Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about.
MCS 270 Spring 2014 Object-Oriented Software Development.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 11 Handling Exceptions and Events.
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.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
Exceptions in Java. What is an exception? An exception is an error condition that changes the normal flow of control in a program Exceptions in Java separates.
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
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Exceptions Exception handling.
Exception Handling How to handle the runtime errors.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Exceptions an unusual condition – e.g. division by zero – e.g. file doesn't exist – e.g. illegal type – etc. etc… typically a run-time error – i.e. during.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Exception handling.
C++ Exceptions.
Exceptions Error Handling and Recovery
EXCEPTION HANDLING IN C++
Why exception handling in C++?
Part IX Fundamentals of C and C++ Programming Exception Handling
EXCEPTION HANDLING.
Chapter 14: Exception Handling
Advanced C++ Exception Handling
Exception Handling.
Exceptions 1 CMSC 202.
Lecture 11 Objectives Learn what an exception is.
Exception and Event Handling
Exception Handling and Event Handling
CMSC 202 Exceptions.
Exception Handling.
Java Programming: From Problem Analysis to Program Design, 4e
CMSC 202 Lesson 20 Exceptions 1.
Presentation transcript:

© Copyright Eliyahu Brutman Exceptions

© Copyright Eliyahu Brutman Exceptions and Design Patterns - 2 Introduction to Exception Handling Definition: An exception is any unusual event, either erroneous or not, detectable by either hardware or software, that may require special processing Without exception handling When an exception occurs, control goes to the operating system, where typically  an error message is displayed  the program is terminated With exception handling Programs are allowed to trap exceptions There is a possibility to fix the problem and continuing execution

© Copyright Eliyahu Brutman Exceptions and Design Patterns - 3 Possible Naïve Solutions Exit the Program Isn ’ t it too strong OS already does the same Return Error Code Need to check the return code for every call Raise a Flag Need to check the flag after every operation Call a Special Code to Correct the Error But when shall we call it?

© Copyright Eliyahu Brutman Exceptions and Design Patterns - 4 Motivation A mechanism which allows two components to survive runtime program anomaly Encountering such an anomaly (division by zero, access to an array outside its bound, etc.), may need an immediate handling The handling may reside in a different component, which we need to communicate The encountering components throws an exception The handling code, catches the exception, and handles it accordingly

© Copyright Eliyahu Brutman Exceptions and Design Patterns - 5 Example Lets consider class stack const int MAX_ELEM=10; template class Stack { Tm_arr[size]; intm_index; public: Stack() : m_index(0) {} void Push(T elemP) { m_arr[m_index] = elemP; m_index++;} T& Pop() { m_index--; return m_arr[m_index]; } int Index() const { return m_index; } };

© Copyright Eliyahu Brutman Exceptions and Design Patterns - 6 Exception Handling Syntax Exception Handlers try { -- code that is expected to raise an exception } catch (formal parameter) { -- handler code }...

© Copyright Eliyahu Brutman Exceptions and Design Patterns - 7 Class Anomalies What happens if we pop() an empty stack? What happens if we push() on a full stack? We decide that these situations need to be handled in a special manner, since they are anomalies of the class behavior. First we define classes of exceptions which may be thrown Upon anomaly detection, we will throw an exception class popOnEmpty { /* … */ } class pushOnFull { /* … */ }

© Copyright Eliyahu Brutman Exceptions and Design Patterns - 8 Example Our code would now change No need to examine the returned value for checking for success. … void Push(T elemP) { if (m_index>=size) throw pushOnFull(); // an object is throws m_arr[m_index] = elemP; m_index++;} T& Pop() { if (m_index<=0) throw popOnEmpty(); // an object is thrown m_index--; return m_arr[m_index]; } … };

© Copyright Eliyahu Brutman Exceptions and Design Patterns - 9 Try…Catch Blocks Wrap the code which needs to be “ exception sensitive ”, and react to raised exceptions, in a try { … } catch { … } blocks The client ’ s code look now as following int main() { try{ Stack si; si.Pop(); si.Push(5); si.Pop(); } catch (popOnEmpty) { cout << "stack is empty !!" << endl; } };

© Copyright Eliyahu Brutman Exceptions and Design Patterns - 10 Try…Catch Blocks When exception is thrown, execution resumes in the “ closest ” catch clause handling the exception. If no catch clause exists capable of handling the exception, program execution resumes at the terminate() function. Variables declared within the try block cannot be referred to at the catch clause, since they are local to the block. int main() { try{ Stack si; si.Pop(); si.Push(5); si.Pop(); } catch (popOnEmpty) { cout << "stack is empty !!" << endl; } cout << “Execution resumes here” << endl; }; Execution does Not resume here Type declaration

© Copyright Eliyahu Brutman Exceptions and Design Patterns - 11 Throwing Objects Why throw an object? Additional data encapsulated An object is created upon invoking the throw statement Can be created with additional data, passed to Ctor Object is destroyed after the catch clause ends

© Copyright Eliyahu Brutman Exceptions and Design Patterns - 12 Stack Unwinding Up the chain of nested function calls, a suitable catch clause is searched for Upon functions and compound statements exit, stack is being unwound Lifetime of local variables ends Destructors are called If no handler is supplied, terminate() is called Since exception is a situation, where the program cannot continue execution in a normal manner By default terminate() calls abort() Similar to function call behavior But information to set up a function call is not available at compile time, run-time support is needed

© Copyright Eliyahu Brutman Exceptions and Design Patterns - 13 Re-throwing and Catch All After some corrective action, a catch clause may throw an exception to be further handled, passing the exception to another catch clause, by throw. General exception handling can be specified catch( … ) { // place code here } If specified with combination, must be specified last, since evaluation is evaluated in turn.

© Copyright Eliyahu Brutman Exceptions and Design Patterns - 14 Exception Specification Function interface need to be as precise as possible, it is the contract between several codes Client code may need to prepare for specific handling Best is to provide exception specification with the method declaration This will help the compiler to check consistency, to check that no other exceptions are thrown It follows the function parameter list

© Copyright Eliyahu Brutman Exceptions and Design Patterns - 15 Exception Specification … void Push(T elemP) throw(pushOnFull) { if (m_index>=size) throw pushOnFull(); // an object is throws m_arr[m_index] = elemP; m_index++;} T& Pop() throw(popOnEmpty) { if (m_index<=0) throw popOnEmpty(); // an object is thrown m_index--; return m_arr[m_index]; } … };

© Copyright Eliyahu Brutman Exceptions and Design Patterns - 16 Nesting try Blocks Exceptions are always handled by closest matching handler try { throw 5; } catch (int x) { cout << x << endl; // exception will be caught here } } catch (int x) { cout << x-5 << endl; }

© Copyright Eliyahu Brutman Exceptions and Design Patterns - 17 Exceptions from Function Calls No different from nested try blocks Auto variables on stack are destroyed Exception jumps to nearest handler Allows exception handlers to be implemented far away from where the problem was May requires a knowledge of the issue packaged in exception object for appropriate handling

© Copyright Eliyahu Brutman Exceptions and Design Patterns - 18 Exception Hierarchies Class types representing exceptions may be organized into groups or hierarchies Class Excp {} Class stackExcp : public Excp {} Class popOnEmpty : public stackExcp {} Class pushOnFull : public stackExcp {} Throwing an exception of type popOnEmpty may be caught by a handler expecting its base Therefore order should be handling more concrete types, and then handling more generic types Since handling is evaluated in order of specification, “ first match ” Ctors and Dtors are called in order like before