EXCEPTION HANDLING IN C++

Slides:



Advertisements
Similar presentations
Topics Introduction Types of Errors Exceptions Exception Handling
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.
CS102--Object Oriented Programming
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.
Review Linked list: Doubly linked list, insertback, insertbefore Remove Search.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 16 Exception Handling.
EXCEPTION HANDLING ► COURSE : MSc Computers and Network engineering. ► Name : Kirthidhar Gandham ► Module : Object Oriented Methods ( ) ► Module.
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.
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.
C++ Exception Handling
Understand Error Handling Software Development Fundamentals LESSON 1.4.
Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  due to poor understanding of the problem and solution.
© Copyright Eliyahu Brutman Exceptions. © Copyright Eliyahu Brutman Exceptions and Design Patterns - 2 Introduction to Exception Handling Definition:
What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic.
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 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.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
(13-1) Exception Handling in C++ D & D Chapter 17 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
1 An Exception is… An unusual, often unpredictable event, detectable by software or hardware, that requires special processing An exception handler is.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
Exception Handling in C++. Outline What exceptions are and when to use them Using try, catch and throw to detect, handle and indicate exceptions, respectively.
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.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Lecture 18B Exception Handling and Richard Gesick.
Namespace יום ראשון 13 נובמבר 2016 יום ראשון 13 נובמבר 2016 יום ראשון 13 נובמבר 2016 יום ראשון 13 נובמבר 2016 יום ראשון 13 נובמבר 2016 יום ראשון 13 נובמבר.
Appendix H Exception Handling: A Deeper Look
Exception handling.
Exception Handling in C++
IS 0020 Program Design and Software Tools
16 Exception Handling.
Chapter 16 Exception Handling
CS212: Object Oriented Analysis and Design
Chapter 14 – Exception Handling
Chapter 10 – Exception Handling
Why exception handling in C++?
Part IX Fundamentals of C and C++ Programming Exception Handling
EXCEPTION HANDLING.
Exceptions C++ Interlude 3
Chapter 14: Exception Handling
Exception Handling and
Exception Handling Chapter 9.
Designing with Java Exception Handling
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 and Reading / Writing Files
Exception Handling Chapter 9 Edited by JJ.
Advanced C++ Exception Handling
Exceptions and Templates
Part B – Structured Exception Handling
Exception Handling.
Programming in C# CHAPTER - 7
Chapter 13 Exception Handling
Lecture 11 Objectives Learn what an exception is.
Errors and Exceptions Error Errors are the wrongs that can make a program to go wrong. An error may produce an incorrect output or may terminate the execution.
Exception and Event Handling
Department of Computer and Information Science, School of Science, IUPUI Exception Handling Dale Roberts, Lecturer Computer Science, IUPUI
Exception Handling and Event Handling
Exceptions for safe programming.
Exception Handling.
Java Programming: From Problem Analysis to Program Design, 4e
Presentation transcript:

EXCEPTION HANDLING IN C++ Bilal Zahoor Kashmir University North Campus

EXCEPTIONS Exceptions are run time anomalies or unusual conditions that a program may encounter during execution. Conditions such as Division by zero Access to an array outside of its bounds Running out of memory Running out of disk space It was not a part of original C++. It is a new feature added to ANSI C++.

EXCEPTION HANDLING Exceptions are of 2 kinds Synchronous Exception: Out of rage Over flow Asynchronous Exception: Error that are caused by causes beyond the control of the program Keyboard interrupts In C++ only synchronous exceptions can be handled.

EXCEPTION HANDLING (CONT…) Exception handling mechanism Find the problem (Hit the exception). Inform that an error has occurred (Throw the exception). Receive the error information (Catch the exception). Take corrective action (handle the exception).

EXCEPTION HANDLING MECHANISM It is basically build upon three keywords try throw catch try block Detects and throw an exception Exception Object catch block Catch and handle the exception

EXCEPTION HANDLING MECHANISM (CONT…) The keyword try is used to preface a block of statements which may generate exceptions. When an exception is detected, it is thrown using a throw statement in the try block. A catch block defined by the keyword ‘catch’ catches the exception and handles it appropriately. The catch block that catches an exception must immediately follow the try block that throws the exception.

EXCEPTION HANDLING MECHANISM (CONT…) try { … // Block of statements // which detect and throws an exception throw exception; … } catch(type arg)

EXCEPTION HANDLING MECHANISM (CONT…) Exceptions are objects used to transmit information about a problem. If the type of the object thrown matches the arg type in the catch statement, the catch block is executed. If they do not match, the program is aborted.

THROWING MECHANISM The throw statement can have one of the following 3 forms throw(exception) throw exception throw //used to re-throw a exception The operand object exception can be of any type, including constant. It is also possible to throw an object not intended for error handling. Throw point can be in a deeply nested scope within a try block or in a deeply nested function call. In any case, control is transferred to the catch statement.

CATCHING MECHANISM catch(type arg) { … } The type indicates the type of exception the catch block handles. The parameter arg is an optional parameter name. The catch statement catches an exception whose type matches with the type of the catch argument. catch(type arg) { … }

CATCHING MECHANISM (CONT…) If the parameter in the catch statement is named, then the parameter can be used in the exception handling code. If a catch statement does not match the exception it is skipped. More than one catch statement can be associated with a try block.

CATCHING MECHANISM (CONT…) try { throw exception; } catch(type1 arg) // catch block 1 catch(type2 arg) // catch block 2 … catch(typeN arg) // catch block N

CATCHING MECHANISM (CONT…) When an exception is thrown, the exception handlers are searched in order for a match. The first handler that yields a match is executed. If several catch statement matches the type of an exception the first handler that matches the exception type is executed. Catch all exception catch (…) { // statement for processing all exceptions }

RETHROWING AN EXCEPTION A handler may decide to rethrow the exception caught without processing it. In such a case we have to invoke throw without any arguments as shown below throw; This causes the current exception to be thrown to the next enclosing try/catch sequence and is caught by a catch statement listed after the enclosing try block