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

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

C++ Namespaces, Exceptions

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

Dr. Md. Humayun Kabir CSE Department, BUET

Programming in C++: Assignment Week 8

Programming in C++: Assignment Week 8

IS 0020 Program Design and Software Tools

! Errors can be dealt with at place error occurs

CPSC 427: Object-Oriented Programming

CS11 Advanced C++ Fall Lecture 3

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

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

17.1 Handling Errors in a Program

Exceptions. Why Exception Handling?

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

Laboratorio di Tecnologie dell'informazione

C++ Programming Fundamentals

C++ Exception Handling 1

Intermediate Programming, Spring 2017*

Object-Oriented Principles and Practice / C++

Assertions and Exceptions

Exception Handling Pearson Education, Inc. All rights reserved.

G52CPP C++ Programming Lecture 16

Exception Handling in C++

A Whirlwind Tour of C++

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

CSCI 104 Exceptions. Mark Redekopp David Kempe

Homework 4. Any questions?

CS102 C++ Exception Handling & Namespaces

Dr. Md. Humayun Kabir CSE Department, BUET

Downloaded from

Stack memory - "scratch pad" memory that is used by automatic variables.

C++_ MARKS 40 MIN

Lecture Material. Exceptions

Exception with arguments

12/2/2009. The plan. References. References vs. pointers. Reference parameters. const and references. HW7 is out; new PM due date Finish last lecture

Strings in C++ Dr. Ferdin Joe John Joseph Kamnoetvidya Science Academy

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

C C++ C++! "# C++ ++

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

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

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

Introduction. Common examples of exceptions

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

LECTURE NOTES ON PROGRAMMING FUNDAMENTAL USING C++ LANGUAGE

05-01 Discussion Notes

CSC 330 Object-Oriented Programming. Exception Handling CSC 330

CS11 Introduction to C++ Fall Lecture 6

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

Exceptions. James Brucker

Internationalization and Error Handling

The University of Nottingham

CONSTRUCTORS AND DESTRUCTORS

VOLUME II CHAPTER 9 INTRODUCTION TO C++ HANDS ON PRACTICE PROGRAMS

Module Operator Overloading and Type Conversion. Table of Contents

WELCOME TO CS 16! Problem Solving with Computers-I

CS Data Structure Spring Answer Key- Assignment #3

CPSC 427: Object-Oriented Programming

C++ is case sensitive language, meaning that the variable first_value, First_Value or FIRST_VALUE will be treated as different.

More Group HW. #ifndef Stackh #define Stackh. #include <cstdlib> using namespace std;

Introduction to RTTI. Jonathan Hoyle Eastman Kodak 11/30/00

04-24/26 Discussion Notes

Chapter-11 POINTERS. Important 3 Marks. Introduction: Memory Utilization of Pointer: Pointer:

The University of Nottingham

D.A.V PUBLIC SCHOOLS, RANCHI ZONE FIRST SUMMATIVE ASSESSMENT CLASS - XI COMPUTER SCIENCE

Programming in C++: Assignment Week 2

G52CPP C++ Programming Lecture 17

Incompatibilities / Differences between C and C++

a data type is Types

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

POINTERS. Pointer is a memory variable which can store address of an object of specified data type. For example:

SFU CMPT Topic: Control Statements

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

TEMPLATES AND EXCEPTION HANDLING

MM1_ doc Page E-1 of 12 Rüdiger Siol :21

C++ basics Getting started with, and Data Types.

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

Exceptions, Templates, and the STL

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

Darshan Institute of Engineering & Technology for Diploma Studies

COMP6771 Advanced C++ Programming

TABLE OF CONTENTS...2 INTRODUCTION...3

Lecture 8. Exceptions, Constructor, Templates TDDD86: DALP. Content. Contents Exceptions

WELCOME TO CS 16! Problem Solving with Computers-I.

Unit 4 Basic Collections

COM S 213 PRELIM EXAMINATION #2 April 26, 2001

This can be thrown by dynamic_cast. This is useful device to handle unexpected exceptions in a C++ program

Operators. The Arrow Operator. The sizeof Operator

CS 162, Lecture 25: Exam II Review. 30 May 2018

CSCI 104 Classes. Mark Redekopp David Kempe

Structured Data. CIS 15 : Spring 2007

Object oriented programming

CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE

3. Functions. Modular programming is the dividing of the entire problem into small sub problems that can be solved by writing separate programs.

CA31-1K DIS. Pointers. TA: You Lu

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

BRAIN INTERNATIONAL SCHOOL. Term-I Class XI Sub: Computer Science Revision Worksheet

Exceptions - Example. Exceptions - Example

Exercise1. // classes first example. #include <iostream> using namespace std; class Rectangle. int width, height; public: void set_values (int,int);

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

Transcription:

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

Exception Handling 2 An exception is an unusual behavior of a program during its execution Exception can occur due to Wrong user input Wong run-time environment Exceptions do not occur always. However, abnormally terminates the program execution when occur Compiler cannot report whether a part of a code will cause exception or not C++ allows programmers to anticipate and handle exceptions

Exception Due to Wrong User Input int main(){ int a, b; cout<<"enter a whole number a: "; cin>>a; cout<<"enter another whole number b: "; cin>>b; cout<<"the result of a/b is: "<<a/b<<endl; //exception if b=0 3

Exception Due to Wrong User Input int main(){ int a, b; cout<<"enter a whole number a: "; cin>>a; cout<<"enter another whole number b: "; cin>>b; if(b!=0){ cout<<"the result of a/b is: "<<a/b<<endl; else { cout<< Can t divide by zero <<endl; 4

Exception Due to Wrong User Input const int dividebyzero=0; int main(){ int a, b; cout<<"enter a whole number a: "; cin>>a; cout<<"enter another whole number b: "; cin>>b; try{ if(b!=0) cout<<"the result of a/b is: "<<a/b<<endl; else throw dividebyzero; catch (int e){ if(e==dividebyzero) cout<< Can t divide by zero <<endl; 5

Exception Due to Wrong User Input 6 int divide(int a, int b){ if(b!=0) return a/b; else cout<< Can t divide by zero <<endl; //will give warning, function ends without returning an integer int main(){ int a, b; cout<<"enter a whole number a: "; cin>>a; cout<<"enter another whole number b: "; cin>>b; cout<<"the result of divide (a, b) is: "<<divide (a,b)<<endl;

Exception Due to Wrong User Input const int dividebyzero=0; 7 int divide(int id a, int b) throw (int){ if(b!=0) return a/b; else throw dividebyzero;

Exception Due to Wrong User Input 8 int main(){ int a, b; cout<<"enter a whole number a: "; cin>>a; cout<<"enter another whole number b: "; cin>>b; try{ cout<<"the result of divide (a, b) is: "<<divide (a,b)<<endl; catch (int e) { if (e==dividebyzero) cout<< Can t divide by zero <<endl;

Exception Due to Wrong Run-time Environment #include <cstring> int main(){ char str[100]="long Live Bangladesh"; char *p; p=new char[100000]; //exception if new cannot allocate memory strcpy(p, str); cout<<str<<endl; cout<<p<<endl; 9

Exception Due to Wrong Run-time Environment #include <cstring> #include <cstdlib> int main(){ char str[100]="long Live Bangladesh"; char *p; p=new char[100000]; if(p==null){ cout<< Memory Allocation Problem <<endl; exit(1); strcpy(p, str); cout<<str<<endl; cout<<p<<endl; 10

Exception Due to Wrong Run-time Environment 11 #include <cstring> #include <cstdlib> int main(){ char str[100]="long Live Bangladesh"; char *p; try { p=new char[100000]; if(p==null) throw Memory Allocation Problem ; catch ( char *str){ cout<< Exception Occurs: <<str<<endl; strcpy(p, str); cout<<str<<endl; cout<<p<<endl;

Exception Due to Wrong Run-time Environment 12 #include <cstring> #include <new> int main(){ char str[100]="long Live Bangladesh"; char *p; try y{ p=new char[100000]; catch ( bad_alloc & ba){ cout<< Exception Occurs: <<ba.what()<<endl; strcpy(p, str); cout<<str<<endl; cout<<p<<endl;

Throwing Primitive Data as Exceptions 13 int main(){ int n; cout<< Enter a number: ; cin>>n; n; try { if (n==0) throw z ; else if(n%2==0) throw 2; else throw 3.14 catch (char c){ cout<< Zero Exception: <<c<<endl; catch (int x){ cout<< Integer Exception: <<x<<endl; catch (double d){ cout<< Double Exception: <<d<<endl;

Catch All Exceptions by Ellipses 14 int main(){ int n; cout<< Enter a number: ; cin>>n; try { if (n==0) throw z ; else if(n%2==0) throw 2; else throw 3.14 catch ( ){ cout<< Exception: <<endl;

Throw User Defined Object as Exception class myclass{ public: char* what() throw(){ return "Divide By Zero Exception"; ; 15

Throw User Defined Object as Exception 16 int main () { int a, b; myclass ex; cout<<"enter a and b: "; cin>>a>>b; try { if(b==0) throw ex; else cout<<"a/b:= "<<a/b<<endl; catch (myclass& e) { cout << "exception caught: " << e.what() << endl;

Standard Exception Class #include <exception> class exception { public: exception () throw(); exception (constt exception&) throw(); exception& operator= (const exception&) throw(); virtual ~exception() throw(); virtual const char* what() const throw(); ; 17

Standard Exception Class All objects thrown by components of the standard library are derived from this exception class Therefore, all standard exceptions can be caught by catching this type Some classes derived from exception class are: bad_alloc bad_cast bad_exception bad_typeid logic_error runtime_error 18

Standard Exception Class 19 #include <typeinfo> class Polymorphic {virtual void Member(){; int main () { try { Polymorphic * pb = 0; typeid(*pb); // throws a bad_typeid exception catch (exception& e) { cerr << "exception caught: " << e.what() << endl;

Standard Exception Class #include <typeinfo> class Base {virtual void Member(){; class Derived : Base {; int main () { try { Base b; Derived& rd = dynamic_cast<derived&>(b); catch (bad_cast& bc) { cerr << "bad_cast caught: " << bc.what() << endl; 20

Standard Exception Class 21 #include <typeinfo> class Base {virtual void Member(){; class Derived : Base {; int main () { try { Base b; Derived& rd = dynamic_cast<derived&>(b); catch (bad_cast& bc) { cerr << "bad_cast caught: " << bc.what() << endl;

User Defined Exception Class Derived from Standard Exception Class #include <exception> class myexception : public exception{ public: const char* what() const throw(){ return "Divide By Zero Exception"; ; 22

User Defined Exception Class Derived from Standard Exception Class int main () { int a, b; myexception ex; cout<<"enter a nd b: "; cin>>a>>b; try { if(b==0) throw ex; else cout<<"a/b:= "<<a/b<<endl; catch (exception& e) {cout << "exception caught: " << e.what() << endl; 23

cout<< Thank You <<endl; cout<< Have a Good Day <<endl; 24