(13-1) exception handling in c++ d & d chapter 17 instructor - andrew s. o’fallon cpts 122...
TRANSCRIPT
![Page 1: (13-1) Exception Handling in C++ D & D Chapter 17 Instructor - Andrew S. O’Fallon CptS 122 Washington State University](https://reader035.vdocuments.net/reader035/viewer/2022062805/5697bfac1a28abf838c9b9b5/html5/thumbnails/1.jpg)
(13-1) Exception Handling in C++D & D Chapter 17
Instructor - Andrew S. O’Fallon
CptS 122
Washington State University
![Page 2: (13-1) Exception Handling in C++ D & D Chapter 17 Instructor - Andrew S. O’Fallon CptS 122 Washington State University](https://reader035.vdocuments.net/reader035/viewer/2022062805/5697bfac1a28abf838c9b9b5/html5/thumbnails/2.jpg)
Key Concepts
Exceptions Exception handling try block catch handler Keyword throw Fault-tolerant programs
A. O’Fallon2
![Page 3: (13-1) Exception Handling in C++ D & D Chapter 17 Instructor - Andrew S. O’Fallon CptS 122 Washington State University](https://reader035.vdocuments.net/reader035/viewer/2022062805/5697bfac1a28abf838c9b9b5/html5/thumbnails/3.jpg)
What is an Exception?
A signal that a problem has occurred during program execution that requires special processing
Exceptions should only occur during “exceptional” circumstances
C++ provides a mechanism for handling exceptions so that programs don’t just “crash” or stop executing, without a chance to recover
A. O’Fallon3
![Page 4: (13-1) Exception Handling in C++ D & D Chapter 17 Instructor - Andrew S. O’Fallon CptS 122 Washington State University](https://reader035.vdocuments.net/reader035/viewer/2022062805/5697bfac1a28abf838c9b9b5/html5/thumbnails/4.jpg)
What is Exception Handling?
A process for detecting and resolving exceptions
C++ exception handling is built on three keywords– try– catch– throw
A. O’Fallon4
![Page 5: (13-1) Exception Handling in C++ D & D Chapter 17 Instructor - Andrew S. O’Fallon CptS 122 Washington State University](https://reader035.vdocuments.net/reader035/viewer/2022062805/5697bfac1a28abf838c9b9b5/html5/thumbnails/5.jpg)
When to Use Exception Handling?
Exception handling processes synchronous errors, which occur when a statement in the program executes
Exception handling does not process asynchronous events that may happen independent of program flow
C. Hundhausen, A. O’Fallon5
![Page 6: (13-1) Exception Handling in C++ D & D Chapter 17 Instructor - Andrew S. O’Fallon CptS 122 Washington State University](https://reader035.vdocuments.net/reader035/viewer/2022062805/5697bfac1a28abf838c9b9b5/html5/thumbnails/6.jpg)
try Block
Contains code that might generate an exception
A. O’Fallon6
![Page 7: (13-1) Exception Handling in C++ D & D Chapter 17 Instructor - Andrew S. O’Fallon CptS 122 Washington State University](https://reader035.vdocuments.net/reader035/viewer/2022062805/5697bfac1a28abf838c9b9b5/html5/thumbnails/7.jpg)
catch Handler
Executes as a result of an exception The correct handler is “activated” when a
match occurs between the type of exception thrown and type of parameter for the handler
An exception parameter should always be declared as a reference to the type of exception in the handler
A. O’Fallon7
![Page 8: (13-1) Exception Handling in C++ D & D Chapter 17 Instructor - Andrew S. O’Fallon CptS 122 Washington State University](https://reader035.vdocuments.net/reader035/viewer/2022062805/5697bfac1a28abf838c9b9b5/html5/thumbnails/8.jpg)
Keyword throw
When an unexpected circumstance occurs an exception is generated by keyword throw
A. O’Fallon8
![Page 9: (13-1) Exception Handling in C++ D & D Chapter 17 Instructor - Andrew S. O’Fallon CptS 122 Washington State University](https://reader035.vdocuments.net/reader035/viewer/2022062805/5697bfac1a28abf838c9b9b5/html5/thumbnails/9.jpg)
Standard Library Exception Classes and Hierarchy (I)
p. 759, Deitel & Deitel, C++ How To Program, 9th Ed.
C. Hundhausen, A. O’Fallon9
![Page 10: (13-1) Exception Handling in C++ D & D Chapter 17 Instructor - Andrew S. O’Fallon CptS 122 Washington State University](https://reader035.vdocuments.net/reader035/viewer/2022062805/5697bfac1a28abf838c9b9b5/html5/thumbnails/10.jpg)
Standard Library Exception Classes and Hierarchy (II)
We can write classes, which are derived from the standard library exception classes (Note: the standard exception classes are located in <stdexcept>)– class DivideByZeroException : public runtime_error
A. O’Fallon10
![Page 11: (13-1) Exception Handling in C++ D & D Chapter 17 Instructor - Andrew S. O’Fallon CptS 122 Washington State University](https://reader035.vdocuments.net/reader035/viewer/2022062805/5697bfac1a28abf838c9b9b5/html5/thumbnails/11.jpg)
Dividing-by-zero Example
Provided in class
C. Hundhausen, A. O’Fallon11
![Page 12: (13-1) Exception Handling in C++ D & D Chapter 17 Instructor - Andrew S. O’Fallon CptS 122 Washington State University](https://reader035.vdocuments.net/reader035/viewer/2022062805/5697bfac1a28abf838c9b9b5/html5/thumbnails/12.jpg)
Fault-tolerant Programs
Programs that can satisfy most, if not all requirements, even if faults or exceptions occur
These programs handle faults or exceptions gracefully, which provides a level of robustness
A. O’Fallon12
![Page 13: (13-1) Exception Handling in C++ D & D Chapter 17 Instructor - Andrew S. O’Fallon CptS 122 Washington State University](https://reader035.vdocuments.net/reader035/viewer/2022062805/5697bfac1a28abf838c9b9b5/html5/thumbnails/13.jpg)
Summary
Exception handling provides a mechanism for building robust and fault-tolerant programs
A. O’Fallon13
![Page 14: (13-1) Exception Handling in C++ D & D Chapter 17 Instructor - Andrew S. O’Fallon CptS 122 Washington State University](https://reader035.vdocuments.net/reader035/viewer/2022062805/5697bfac1a28abf838c9b9b5/html5/thumbnails/14.jpg)
A. O’Fallon14
References
P.J. Deitel & H.M. Deitel, C++ How to Program (9th Ed.), Pearson Education , Inc., 2014.
J.R. Hanly & E.B. Koffman, Problem Solving and Program Design in C (7th Ed.), Addison-Wesley, 2013