functions and recursion in c++lecture.riazulislam.com/uploads/3/9/8/5/3985970/slides_20.pdfc++...

18
Exception Handling

Upload: others

Post on 14-Oct-2020

15 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Functions and Recursion in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/slides_20.pdfC++ Standard Exceptions • exception: An exception and parent class of all the standard

Exception Handling

Page 2: Functions and Recursion in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/slides_20.pdfC++ Standard Exceptions • exception: An exception and parent class of all the standard

Contents

• Overview of Exception Handling

• Basic Structure of Try, Throw, and Catch Model w. Examples

• Rethrowing an Exception

• C++ Standard Exceptions and Exception Specifications

• Stack Unwinding

Page 3: Functions and Recursion in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/slides_20.pdfC++ Standard Exceptions • exception: An exception and parent class of all the standard

Exception Handling Overview

• Intermixing program logic with error handling logic can make the program difficult to read, modify, maintain and debug.

• Exception handling enables you to remove error-handling code from the “main line” of the program’s execution.

• Choose—all exceptions, all exceptions of a certain type or all exceptions of a group of related types.

Page 4: Functions and Recursion in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/slides_20.pdfC++ Standard Exceptions • exception: An exception and parent class of all the standard

Exception: Try, throw, catch MODEL

Exception Parameter

Page 5: Functions and Recursion in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/slides_20.pdfC++ Standard Exceptions • exception: An exception and parent class of all the standard

Throw Point

Termination Model of Exception Handling

Page 6: Functions and Recursion in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/slides_20.pdfC++ Standard Exceptions • exception: An exception and parent class of all the standard

Termination Model of Exception Handling

Also, called Resumption Model

• Throw Point: Program control does not return to the point at which the exception occurred because the try block has expired.

• Control resumes with the first statement after the last catch handler following the try block

Page 7: Functions and Recursion in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/slides_20.pdfC++ Standard Exceptions • exception: An exception and parent class of all the standard

Example: Division by ZERO

// what() member function

Page 8: Functions and Recursion in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/slides_20.pdfC++ Standard Exceptions • exception: An exception and parent class of all the standard

Example: Division by

ZERO

Page 9: Functions and Recursion in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/slides_20.pdfC++ Standard Exceptions • exception: An exception and parent class of all the standard

Example: Division by ZERO

Page 10: Functions and Recursion in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/slides_20.pdfC++ Standard Exceptions • exception: An exception and parent class of all the standard

Exception Handling: When to Use

• For synchronous errors, which occur when a statement executes.

Examples: errors are out-of-range array subscripts, arithmetic overflow, division by zero, invalid function parameters and unsuccessful memory allocation.

• Not for asynchronous events

Examples: Disk I/O completions, network message arrivals, mouse clicks and keystrokes.

• Also, for processing problems that occur when a program interacts with software elements, such as member functions, constructors, destructors and classes

Page 11: Functions and Recursion in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/slides_20.pdfC++ Standard Exceptions • exception: An exception and parent class of all the standard

Rethrowing an Exception

• An exception handler, might decide either that it cannot process that exception orthat it can process the exception only partially.

• The exception handler can defer the exception handling to another exception handler by rethrowing the exception

Note: An exceptional handler can call C++ standard exceptions.

Page 12: Functions and Recursion in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/slides_20.pdfC++ Standard Exceptions • exception: An exception and parent class of all the standard

C++ Standard Exceptions

• exception: An exception and parent class of all the standard C++ exceptions.

• logic_error: An exception that theoretically can be detected by reading the code.

• runtime_error: An exception that theoretically can not be detected by reading the code.

Page 13: Functions and Recursion in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/slides_20.pdfC++ Standard Exceptions • exception: An exception and parent class of all the standard

Example: Rethrowingan Exception

Page 14: Functions and Recursion in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/slides_20.pdfC++ Standard Exceptions • exception: An exception and parent class of all the standard

Exception Specifications

• An optional exception specification enumerates a list of exceptions that a function can throw

• unexpected function call: If the function throws an exception that does not belong to a specified type. (program termination)

• No exception: an empty exception specification

throw()

throw list

??How to process unexpected ExceptionsDo Yourself

Page 15: Functions and Recursion in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/slides_20.pdfC++ Standard Exceptions • exception: An exception and parent class of all the standard

Stack Unwinding

• A call stack is a stack data structure that stores information about the active functions.

• The main reason for having a call stack is to keep track of the point to which each active function should return control when it completes executing.

Page 16: Functions and Recursion in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/slides_20.pdfC++ Standard Exceptions • exception: An exception and parent class of all the standard

Stack Unwinding

Page 17: Functions and Recursion in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/slides_20.pdfC++ Standard Exceptions • exception: An exception and parent class of all the standard

Example: Stack Unwinding

Page 18: Functions and Recursion in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/slides_20.pdfC++ Standard Exceptions • exception: An exception and parent class of all the standard

Example: Stack Unwinding