Introduction. Common examples of exceptions

Similar documents
! Errors can be dealt with at place error occurs

Exception Handling Pearson Education, Inc. All rights reserved.

IS 0020 Program Design and Software Tools

Exception Handling Pearson Education, Inc. All rights reserved.

CSC 330 Object-Oriented Programming. Exception Handling CSC 330

CS11 Advanced C++ Fall Lecture 3

C++ Crash Kurs. Exceptions. Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck

Exception Handling in C++

void fun() C::C() // ctor try try try : member( ) catch (const E& e) { catch (const E& e) { catch (const E& e) {

Lecture 10. To use try, throw, and catch Constructors and destructors Standard exception hierarchy new failures

C++ Exception Handling 1

Contents. Error Handling Strategies (cont d) Error handling strategies: error code, assert(), throw-try-catch

Unit 4 Basic Collections

Exceptions. Why Exception Handling?

Assertions and Exceptions

Throwing exceptions 02/12/2018. Throwing objects. Exceptions

CSCI 104 Exceptions. Mark Redekopp David Kempe

mywbut.com Exception Handling

CS102 C++ Exception Handling & Namespaces

Object-Oriented Principles and Practice / C++

Lecture Material. Exceptions

Chapter 16: Exceptions, Templates, and the Standard Template Library (STL)

Traditional Error Handling

17.1 Handling Errors in a Program

C++ TEMPLATES. Templates are the foundation of generic programming, which involves writing code in a way that is independent of any particular type.

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming

CSC 330. An Exception is. Handling Exceptions. Error Handling. Assertions. C++ Exception. Example

Exception handling. Aishy Amer. Lecture Outline: Introduction Types or errors or exceptions Conventional error processing Exception handling in C++

05-01 Discussion Notes

Intermediate Programming, Spring 2017*

COMP6771 Advanced C++ Programming

Laboratorio di Tecnologie dell'informazione

Object Oriented Programming CS250

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

TEMPLATES AND EXCEPTION HANDLING

! An exception is a condition that occurs at execution time and makes normal continuation of the program impossible.

Le L c e t c ur u e e 5 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Exception Handling

What can go wrong in a Java program while running?

CSCI 104 Classes. Mark Redekopp David Kempe

Exception Handling Introduction. Error-Prevention Tip 13.1 OBJECTIVES

21. Exceptions. Advanced Concepts: // exceptions #include <iostream> using namespace std;

C++ Programming Fundamentals

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

04-24/26 Discussion Notes

Operators. The Arrow Operator. The sizeof Operator

C++ PROGRAMMING LANGUAGE: DYNAMIC MEMORY ALLOCATION AND EXCEPTION IN C++. CAAM 519, CHAPTER 15

Exception Handling Alternatives (Part 2)

Exception with arguments

Programming in C++: Assignment Week 8

Object-Oriented Programming for Scientific Computing

Exceptions, Case Study-Exception handling in C++.

CS11 Introduction to C++ Fall Lecture 6

2. How many runtime error messages associated with exception? a) 2 b) 4 c) 5 d) infinite

Outline. Zoltán Porkoláb: C++11/14 1

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

ECE 122. Engineering Problem Solving with Java

C++ Programming Lecture 5 Software Engineering Group

CPSC 427: Object-Oriented Programming

Chapter 12 Exception Handling

A Whirlwind Tour of C++

CS S-22 Exceptions 1. Running a web server, don t want one piece of bad data to bring the whole thing down

DE70/DC56/DE122/DC106 OBJECT ORIENTED PROGRAMMING WITH C++ DEC 2015

This examination has 11 pages. Check that you have a complete paper.

Chapter 5. Object- Oriented Programming Part I

Exception Safe Coding

Administration. Exceptions. Leftovers. Agenda. When Things Go Wrong. Handling Errors. CS 99 Summer 2000 Michael Clarkson Lecture 11

More on Templates. Shahram Rahatlou. Corso di Programmazione++

Introduction to Java. Handout-3a. cs402 - Spring

Exceptions. CS162: Introduction to Computer Science II. Exceptions. Exceptions. Exceptions. Exceptions. Exceptions

Informatica 3. Marcello Restelli. Laurea in Ingegneria Informatica Politecnico di Milano 9/15/07 10/29/07

JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli (An approved by AICTE and Affiliated to Anna University)

Internationalization and Error Handling

CS313D: ADVANCED PROGRAMMING LANGUAGE

Exception-Handling Overview

Error Handling in C++

1 Introduction to C++ C++ basics, simple IO, and error handling

Practice test for midterm 3 solutions

Jayaram college of Engineering and Technology, Pagalavadi. CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT

CPA Chapter 7 Practice Quiz. C++ Institute Volunteer Program

Programming C++ Lecture 6. Howest, Fall 2013 Instructor: Dr. Jennifer B. Sartor

Programming with C++ Exceptions

Lecture 3 Tao Wang 1

Templates and Vectors

Module 7 b. -Namespaces -Exceptions handling

Introduction to Computer Science II CS S-22 Exceptions

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011

C++ Exception Handling. Dr. Md. Humayun Kabir CSE Department, BUET

COMP322 - Introduction to C++ Lecture 10 - Overloading Operators and Exceptions

Programming in C++: Programming Test-1

Module 1. C++ Classes Exercises

Handling Exceptions. Introduction. Using the Program Halt Handler CHAPTER 10

Chapter 11 Handling Exceptions and Events. Chapter Objectives

ECE 462 Midterm Exam 2. 10:30-11:20AM, October 19, 2007

Program Correctness and Efficiency. Chapter 2

G52CPP C++ Programming Lecture 16

CS 209 Programming in Java #10 Exception Handling

Chapter 5 Errors. Bjarne Stroustrup

Java Errors and Exceptions. Because Murphy s Law never fails

Exceptions. Gunnar Gotshalks EX 1

Transcription:

Exception Handling

Introduction Common examples of exceptions Failure of new to obtain memory Out-of-bounds array subscript Division by zero Invalid function parameters Programs with exception handling Clearer, more robust, more fault-tolerant Can recover from errors Can shut down gracefully

Basics of C++ Exception Handling Enclose the code that may generate an error in a try block Follow try block with catch blocks Function that detects an error is unable to deal with it Just throw an exception No guarantee the exception will be handled The thrown object is typically a string or a class object

Exception-Handling Example #include<iostream> class DivBy0 { public: DivBy0():msg( attempted to divide by0 ){ const char *what() const {return msg; private: const char *msg; ; double quotient(int oper1, int oper2){ if (oper2==0) throw DivBy0(); return static_cast<double>(oper1)/oper2;

Exception-Handling Example void main() { int num1, num2; double result; while (cin >> num1 >> num2) { try{ result = quotient(num1, num2); cout << result is: <<result<<endl; catch (DivBy0 ex) { cout << Exception: <<ex.what<< \n ;

Throwing an Exception Throw indicates an exception has occurred throwing an exception Normally specifies one operand of any type A temporary copy of the throw operand is created, it is destroyed when the exception handler complete execution

Catching an Exception Exception handlers are contained in catch blocks catchis followed by parentheses containing a type and an optional parameter name The first one after the try block that matches the type of thrown object catches exception An exception that is not caught causes a call to terminate, by default abort catch( )means to catch all exceptions Must be placed last

Match Occurs When The type of the catch handler A matches the thrown object B They are indeed of the same type A is a public base class of the class of B A is of a base-class pointer/reference and B is of a derived-class pointer/reference The handler is catch( )

Rethrowing an Exception Handler can rethrow the exception Unable to process Just release resources void throwexception() { try { cout << Function throwexception\n ; throw exception(); catch (exception e) { cout << Exception handled\n ; throw; cout << I am here 2\n ;

Rethrowing an Exception void main() { try { throwexception(); cout << I am here 1\n ; catch (exception e) { cout << Exception handled in main\n ; cout << Control continues\n ; Function throwexception Exception handled Exception handled in main Control continues

Exception Specification It lists the exception that can be thrown A function may throw the indicated exceptions or derived types with public inheritance. If an exception not listed is thrown, function unexpected is called. unexpected calls the function specified with set_unexpected function. If no function specified, terminate is called. Prototype of set_unexpected: <exception>

Exception Specification // can only throw a, b, or c void foo(double h) throw (a, b, c) { //function body //an empty exception specification will not //throw any exceptions void foo(double h) throw () { //function body //a function with no exception specification //can throw any exceptions void foo(double h) { //function body

Stack Unwinding #include <stdexcept> void foo3() throw (runtime_error) { throw runtime_error( error in foo3 ); void foo2() throw (runtime_error) { foo3(); void foo1() throw (runtime_error) { foo2();

Stack Unwinding void main(){ try{ foo1(); catch (runtime_error e) { cout << Exception: <<e.what()<<endl; Exception: error in foo3

Standard Exception Hierarchy Base class exception (<exception>) contains what() runtime_error: errors detected at execution time (<stdexcept>) overflow_error, underflow_error logic_error: error in logic that can be prevented by proper coding(<stdexcept>) Invalid_argument, out_of_range

When Exception Handling Should be Used It can be used as an alternative to program control However, should be used to process only exceptional situations