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.

Slides:



Advertisements
Similar presentations
Topics Introduction Types of Errors Exceptions Exception Handling
Advertisements

C/c++ 4 Yeting Ge.
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.
Mahmoud Rafeek Alfarra Computer Programming || Chapter 2: Exception handling.
Lesson 16 Exceptions Lesson Exceptions1. Murphy’s Law Anything that can go wrong will go wrong Lesson Exceptions2.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  due to poor understanding of the problem and solution.
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
C++ Exceptions STL Vector. Example int Quotient (int numer, int denom} { if (denom != 0) return (numer/denom); else //What to do?? }
UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.
1 CSC241: Object Oriented Programming Lecture No 27.
BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class.
Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  Due to poor understanding of the problem and solution.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
HANDLING EXCEPTIONS Chapter 9. Outline  Learn about the limitations of traditional error-handling methods  Throw exceptions  Use try blocks  Catch.
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 Outline 23.1 Introduction
CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Exceptions Exception handling.
C++ Namespaces, Exceptions CSci 588: Data Structures, Algorithms and Software Design All material not from online sources copyright © Travis Desell, 2011.
Exception Handling How to handle the runtime errors.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 Object-Oriented Programming in C++ Exception.
C++ Lesson 1.
C ++ MULTIPLE CHOICE QUESTION
Exception handling.
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
CMSC202 Computer Science II for Majors Lecture 16 – Exceptions
CS 2704 Object Oriented Software Design and Construction
Andy Wang Object Oriented Programming in C++ COP 3330
Jim Fawcett CSE687 – Object Oriented Design Spring 2001
Jim Fawcett CSE687 – Object Oriented Design Spring 2015
Object-Oriented Programming (OOP) Lecture No. 45
Why exception handling in C++?
Part IX Fundamentals of C and C++ Programming Exception Handling
Indexer AKEEL AHMED.
EXCEPTION HANDLING.
Object Oriented Programming COP3330 / CGS5409
2.3 Testing Programs 12 – Program Correctness.
Polymorphism Lec
EE422C Software Implementation II
Designing with Java Exception Handling
Exceptions with Functions
Chapter 17 Templates and Exceptions Part 2
Throwing exceptions.
Chapter 12 Exception Handling and Text IO
Exceptions Problems in a Java program may cause exceptions or errors representing unusual or invalid processing. An exception is an object that defines.
Exception Handling Chapter 9 Edited by JJ.
Throwing and catching exceptions
Andy Wang Object Oriented Programming in C++ COP 3330
Exceptions CSCE 121 J. Michael Moore
Throwing exceptions.
CSC 270 – Survey of Programming Languages
Designing with Java Exception Handling
Object-Oriented Programming (OOP) Lecture No. 44
Lecture 9.
Exceptions for safe programming.
Exception Handling.
C++ Polymorphism Reference and pointer implicit type casting
ENERGY 211 / CME 211 Lecture 24 November 14, 2008.
Throwing exceptions.
Presentation transcript:

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 its sub classes. Classes inheriting an Abstract Class must provide definition to the pure virtual function, otherwise they will also become abstract class.

Characteristics of Abstract Class Abstract class cannot be instantiated, but pointers and references of Abstract class type can be created. Abstract class can have normal functions and variables along with a pure virtual function. Abstract classes are mainly used for Up casting, so that its derived classes can use its interface. Classes inheriting an Abstract Class must implement all pure virtual functions, or else they will become Abstract too.

Exception Handling

An exception is a problem that arises during the execution of a program. A C++ exception is a response to an exceptional circumstance that arises while a program is running, such as an attempt to divide by zero.

Exceptions provide a way to transfer control from one part of a program to another. C++ exception handling is built upon three keywords: try, catch, and throw. throw: A program throws an exception when a problem shows up. This is done using a throw keyword. catch: A program catches an exception with an exception handler at the place in a program where you want to handle the problem. The catch keyword indicates the catching of an exception. try: A try block identifies a block of code for which particular exceptions will be activated. It's followed by one or more catch blocks.

Syntax try { throw e1; } catch( ExceptionName e1 ) { // catch block catch( ExceptionName en )

#include <iostream> using namespace std;   int main() {    int x = -1;    // Some code    cout << "Before try \n";    try {       cout << "Inside try \n";       if (x < 0)       {          throw x;          cout << "After throw (Never executed) \n";       }    }    catch (int x ) {       cout << "Exception Caught \n";    cout << "After catch (Will be executed) \n";    return 0; }

There is a special catch block called ‘catch all’ catch(…) that can be used to catch all types of exceptions. For example, in the following program, an int is thrown as an exception, but there is no catch block for int, so catch(…) block will be executed. #include <iostream> using namespace std;   int main() {     try  {        throw 10;     }     catch (char *excp)  {         cout << "Caught " << excp;     catch (...)  {         cout << "Default Exception\n";     return 0; }

Implicit type conversion doesn’t happen for primitive types Implicit type conversion doesn’t happen for primitive types. For example, in the following program ‘a’ is not implicitly converted to int #include <iostream> using namespace std;   int main() {     try  {        throw 'a';     }     catch (int x)  {         cout << "Caught " << x;     catch (...)  {         cout << "Default Exception\n";     return 0; }

C++, try-catch blocks can be nested C++, try-catch blocks can be nested. Also, an exception can be re-thrown using “throw; ” #include <iostream> using namespace std;   int main() {     try {         try  {             throw 20;         }         catch (int n) {              cout << "Handle Partially ";              throw;   //Re-throwing an exception     }     catch (int n) {         cout << "Handle remaining ";     return 0; }

Standard Exceptions C++ provides a list of standard exceptions defined in <exception> which we can use in our programs.

Exception Description std::exception An exception and parent class of all the standard C++ exceptions. std::bad_alloc This can be thrown by new. std::bad_cast This can be thrown by dynamic_cast. std::bad_exception This is useful device to handle unexpected exceptions in a C++ program std::bad_typeid This can be thrown by typeid. std::logic_error An exception that theoretically can be detected by reading the code. std::domain_error This is an exception thrown when a mathematically invalid domain is used std::invalid_argument This is thrown due to invalid arguments. std::length_error This is thrown when a too big std::string is created

Exception Description std::out_of_range This can be thrown by the at method from for example a std::vector and std::bitset<>::operator[](). std::runtime_error An exception that theoretically can not be detected by reading the code. std::overflow_error This is thrown if a mathematical overflow occurs. std::range_error This is occured when you try to store a value which is out of range. std::underflow_error This is thrown if a mathematical underflow occurs.