ebony
Uploaded by
16 SLIDES
305 VUES
160LIKES

Understanding C++ Exceptions: Handling Errors with Control Flow

DESCRIPTION

This overview explores C++ exceptions, focusing on how program control flow is interrupted when an exception is thrown. We discuss the unwinding of the call stack, resource management, and how to properly handle exceptions using try/catch blocks. An example illustrates the challenge of division by zero within a class method and highlights the importance of exceptions over alternative error-handling methods. The syntax for throwing and catching exceptions is introduced, and we examine the stack frame structure, detailing its components and significance in function call management.

1 / 16

Télécharger la présentation

Understanding C++ Exceptions: Handling Errors with Control Flow

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript

Playing audio...

    1. Overview of C++ Exceptions Normal program control flow is halted At the point where an exception is thrown The program call stack unwinds Stack frame of each function in call chain pops Variables in each popped frame are destroyed Goes until an enclosing try/catch scope is reached Control passes to first matching catch block Can handle the exception and continue from there Can free some resources and re-throw exception

    2. C++ Exceptions Example Consider a program using a Number class Weve not covered class member functions in detail yet Class member functions are much like plain-old functions But are given a hidden extra pointer to the object (this) One of the class member functions is operator /= Divides value object contains by another passed value Problem: what if someone passes 0 to this function?

    3. Motivation for C++ Exceptions void Number::operator/=(const double denom) { if (denom == 0.0) { // what do we do here? } m_value /= denom; } Need to handle divide by zero case Otherwise bad things happen Program crash Wrong results Could set value to NaN Special not-a-number value But, caller might fail to check for it Could return a special value Might be a valid return value Could still be ignored Exceptions offer a better alternative

    4. C++ Exception Syntax void foo() throw (int) { throw 2; } catch (int &i) { cout << i << endl; } catch (...) { cout << another exception << endl; } Can throw any type Can specify what a function (only) throws in its declaration Can catch and use exceptions in code Default catch block

    5. Whats in a Stack Frame? In general, structure is common A chunk of memory representing the state of an active function call Pushed on program call stack at run-time g++ -s gives assembler output can be used to deduce exact structure for a given platform Contains: The previous frame pointer The return address for the call (i.e., just after the point from which it was called) Parameters passed to the function Automatic (stack) variables for the function

More Related