Exceptions for safe programming.

Slides:



Advertisements
Similar presentations
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Advertisements

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
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.
1 Chapter 17-2 Templates and Exceptions Dale/Weems.
Exceptions, Templates, And The Standard Template Library (STL) Chapter 16.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
Review Linked list: Doubly linked list, insertback, insertbefore Remove Search.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 16 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.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 16: Exceptions, Templates, and the Standard Template Library (STL)
SE-1020 Dr. Mark L. Hornick 1 Exceptions and Exception Handling.
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 One benefit of C++ over C is its exception handling system. An exception is a situation in which a program has an unexpected.
Jerry Lebowitz. Topics  Provides a facility for a systematic object oriented approach to handling runtime errors ◦ Can also handle runtime errors.
1 CSC241: Object Oriented Programming Lecture No 28.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 16 Exception Handling.
© Copyright Eliyahu Brutman Exceptions. © Copyright Eliyahu Brutman Exceptions and Design Patterns - 2 Introduction to Exception Handling Definition:
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
CPSC 252 Exception Handling Page 1 Exceptions and exception handling Client programmers can make errors using a class attempting to dequeue an item from.
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.
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 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.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
Exception Handling Outline 23.1 Introduction
CSCI 383 Object-Oriented Programming & Design Lecture 20 Martin van Bommel.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Exceptions Exception handling.
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.
Exception handling.
C++ Exceptions.
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.
CS212: Object Oriented Analysis and Design
EXCEPTION HANDLING IN C++
Andy Wang Object Oriented Programming in C++ COP 3330
Chapter 10 – Exception Handling
Exceptions, Templates, and the Standard Template Library (STL)
Why exception handling in C++?
Part IX Fundamentals of C and C++ Programming Exception Handling
EXCEPTION HANDLING.
Object Oriented Programming COP3330 / CGS5409
Fall 2017 CISC124 9/21/2018 CISC124 First onQ quiz this week – write in lab. More details in last Wednesday’s lecture. Repeated: The quiz availability.
CMSC 202 Lesson 21 Exceptions II.
Exceptions C++ Interlude 3
Chapter 14: Exception Handling
Exception Handling Chapter 9.
Exceptions with Functions
Andy Wang Object Oriented Programming in C++ COP 3330
Chapter 17 Templates and Exceptions Part 2
Exception Handling Chapter 9 Edited by JJ.
Exception Handling In Text: Chapter 14.
Exceptions and Templates
Exception Handling.
Exceptions 1 CMSC 202.
Standard Version of Starting Out with C++, 4th Edition
Exceptions, Templates, and the Standard Template Library (STL)
Exception and Event Handling
Object-Oriented Programming (OOP) Lecture No. 44
Exceptions 10-May-19.
Lecture 9.
Exception Handling.
ENERGY 211 / CME 211 Lecture 24 November 14, 2008.
CMSC 202 Lesson 20 Exceptions 1.
Presentation transcript:

Exceptions for safe programming

Why Exceptions exceptions are used to signal that something unusual has happened during program execution when signal is sent, the control is transferred to specific place in a program designated to handle this exception throwing the exception – the event of sending a signal catching the exception – handling it exception mechanism is for handling extraordinary situations: division by zero, array out of range do not use it for ordinary transfer of control – bad style, cryptic program

Syntax two constructs try block – defines the program area where an exception may be thrown try { // some code throw ARGUMENT; // throwing exception // more code } catch block (exception handler) – processes the exception catch (TYPE PARAMETER){ // exception handling code can be multiple catch blocks, distinguished by exception type default exception catches all un-caught exceptions catch (...){

Operation when the exception is thrown, control is transferred to the first catch block outside the try-block that matches the type of the thrown exception after catch-block finishes, control is passed to the first statement past the last catch-block

Objects as Exceptions classes can be used as exception types can give exception a descriptive name can be used to pass details about exception example empty class to give descriptive name class divide_by_zero {}; more details to be passed class wrongNumber{ ... private: int offendingNumber; }

Catching Outside Functions may be useful to handle the exception outside the function where it is thrown function is said to throw the exception by default function may throw any exception function prototype/head may list exceptions thrown by function, if throws anything else – special unexpected () function is invoked (by default terminates program) syntax returnVal functionName(parameterList) throw (exceptionType); example double safe_divide(int n, int d) throw (divide_by_zero){ if (d == 0) throw divide_by_zero(); return n/double(d); }

Catching Standard Exceptions some pre-defined functions throw standard exceptions need stdexcept header at() – throws out_of_range new – throws bad_alloc – unsuccessful allocation, possibly out of memory example handling try{ string s=”hello”; s.at(5)=’!’; // throws out_of_range } catch(out_of_range){ cout << ”string too short!”;

Questions on Exceptions what are exceptions why are they needed? what are try-block? catch-block? What does it mean to throw or catch an exception? what is the type of an exception? what is the default exception? how is it caught? can an object be used to throw an exception? why is that useful? how can an exception be thrown inside a function and caught outside it? what are standard exceptions? What standard exceptions have we studied?