Review Linked list: Doubly linked list, insertback, insertbefore Remove Search.

Slides:



Advertisements
Similar presentations
Pearson Education, Inc. All rights reserved. 1.. Exception Handling.
Advertisements

Topics Introduction Types of Errors Exceptions Exception Handling
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
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.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 16 Exception Handling.
 2006 Pearson Education, Inc. All rights reserved. Exception Handling in C++ CS-2303, C-Term Exception Handling in C++ CS-2303 System Programming.
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.
Chapter 8Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 8 l Basic Exception Handling »the mechanics of exceptions l.
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.
C++ Exception Handling
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 16 Exception Handling.
Understand Error Handling Software Development Fundamentals LESSON 1.4.
CSI 3120, Exception handling, page 1 Exception and Event Handling Credits Robert W. Sebesta, Concepts of Programming Languages, 8 th ed., 2007 Dr. Nathalie.
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
Slides prepared by Rose Williams, Binghamton University Chapter 9 Exception Handling.
Exceptions Used to signal errors or unexpected situations to calling code Should not be used for problems that can be dealt with reasonably within local.
© Copyright Eliyahu Brutman Exceptions. © Copyright Eliyahu Brutman Exceptions and Design Patterns - 2 Introduction to Exception Handling Definition:
 2002 Prentice Hall. All rights reserved Exception-Handling Overview Exception handling –improves program clarity and modifiability by removing.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
1 Exception and Event Handling (Based on:Concepts of Programming Languages, 8 th edition, by Robert W. Sebesta, 2007)
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 9 : Exception Handling King Fahd University of Petroleum & Minerals College of Computer.
Dr. Abraham. Exception Any problem that VB or OS could not handle Robust program A program that performs well not only under ordinary conditions but also.
Object Oriented Programming
CIS 270—Application Development II Chapter 13—Exception Handling.
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.
Slides Credit Umair Javed LUMS Web Application Development.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Exceptions Handling the unexpected. RHS – SWC 2 The Real World So far, most of our code has been somewhat näive We have assumed that nothing goes wrong…
VB.Net - Exceptions Copyright © Martin Schray
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 10.
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.
Computer Programming with JAVA Chapter 8. Exception Handling Basic Exception Handling the mechanics of exceptions Defining and Using Exceptions some "simple"
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
Exception Handling Fall 2008 Dr. David A. Gaitros
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.
CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling.
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.
1 Handling Errors and Exceptions Chapter 6. 2 Objectives You will be able to: 1. Use the try, catch, and finally statements to handle exceptions. 2. Raise.
Lecture 18B Exception Handling and Richard Gesick.
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 in C++
16 Exception Handling.
Chapter 16 Exception Handling
Andy Wang Object Oriented Programming in C++ COP 3330
Why exception handling in C++?
Object Oriented Programming COP3330 / CGS5409
Exceptions C++ Interlude 3
Chapter 14: Exception Handling
Exception Handling and
Andy Wang Object Oriented Programming in C++ COP 3330
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.
Exception Handling Imran Rashid CTO at ManiWeber Technologies.
Exceptions for safe programming.
CMSC 202 Exceptions.
Exception Handling.
Presentation transcript:

Review Linked list: Doubly linked list, insertback, insertbefore Remove Search

Exception handling

C++ exception handling Exception: an error or problem condition E.g. divide by zero, access NULL pointer, etc Exception handling: dealing with error or problem conditions C++ has some built-in mechanism for exception handling Without using the built-in support for exception handling, one can can such situations by adding checks in the code. See sample1.cpp Handling the exception: sample2.cpp in the traditional way One can live without using C++ exception handling supports: just use the good old if-statement as shown in the example.

Why exception handling? Typical error-checking using the if-statement intermixes error handling code with the tasks of a program Many potential problems happen very infrequently. Code to handle exceptions ideally should not intermix with the main program logic (making it hard to read and debug). With exception handling: code to handle exceptions is separated from the main program logic improves program clarity. Exception handling also often improves program’s fault tolerance – with a more systematic method to handle errors.

When to use exception handling? Exception handling is not always appropriate for handling exceptions E.g. conventional method is better for input checking. When it is good? Problems that occur infrequently. Problems that do not need to be handled in the same block Good for setting up uniform techniques for error-handling when many programmers work on multiple modules.

C++ exception handling The try-throw-catch blocks try { … code to try … possibly throw exceptions } catch (type1 catch_parameter_1) { … code to handle type1 exception} // called exception handler catch (type2 catch _parameter_2) { … code to handle type2 exception}

The try block Syntax: Try {.. the main logic, possibly throw exceptions } Contains code when everything goes smoothly The code however may have exceptions, so we want to “give it a try” If something unusual happens, the way to indicate it is to throw an exception.

Throw statement Syntax: throw expression_for_value_to_be_thrown; Semantic: This indicates that an exception has happens If a throw statement is executed, the try block immediately ends The program attempts to match the exception to one of the catch blocks (which contains code for exception handlers) based on the type of the value thrown (expression_for_value_to_be_thrown) If a match is found, the code in the catch block executes Only one catch block will be matched if any. Program then resumes after the last catch block. Both the exception value and the control flow are transferred (thrown) to the catch block (exception handler).

Catch block 1 or more catch blocks follow a try block. Each catch block has a single parameter with type each catch block is an exception handler (handling exceptions of one type) catch (type catch_block_parameter) { … exception handler code } Catch_block_parameter catches the value of the exception thrown, and can be used in the exception handler code. The exception thrown matches one of the catch parameter If not, you have a un-caught exception situation.

Try-throw-catch summary Normally runs the try block then the code after the last catch block. If the try block throws an exception (run a throw statement) 1. The try block stops immediately after the throw statement 2. The code in the catch block starts executing with the throw value passed as the catch block parameter. 3. After the catch block completes, the code after the catch blocks starts executing. See sample3.cpp If exception is thrown but not caught, then terminate() will be called – the function terminates the program. See sample3a.cpp

Multiple throws and catches Each catch block catches one type of exceptions Need multiple catch blocks When the value is not important, the parameter can be omitted. E.g. catch(int) {…} catch (…) catches any exception, can serve as the default exception handler See sample4.cpp

Exception classes It is common to define classes just to handle exceptions. One can have a different class to deal with a different class of exceptions. Exception classes are just regular classes They are just used for the exception handling purpose. See sample5.cpp

The C++ standard built-in exception class In C++, there is a standard library with pre-built exception classes. The primary base class is called exception, and comes from here: #include using std::exception; Your own exception class can be built from the standard exception class. See sample6.cpp All exceptions thrown from routines in C++ standard libraries.

Throwing an exception in a function So far, exceptions are thrown and caught in the same level in the samples. In practice, programs are modularized with routines Routines may be need to throw an exception that is catch in other routines See sample7.cpp

Throwing an exception in a function Function with potential to throw exceptions may behave in a strange manner. C++ allows specific potential exceptions for each routine using the exception specification. double safedivide(int top, int bottom) throw (int); Can only throw the int type exceptions to the outside double safedivide1(int top, int bottom); No exception list: the function can throw any exception. double safedivide(int top, int bottom) throw (); Empty exception list: the function cannot throw any exception.