0% found this document useful (0 votes)
50 views17 pages

Stack Unwinding

The document discusses stack unwinding in C++. It explains that stack unwinding involves calling destructors when an exception is thrown to remove function entries from the call stack. It also covers try, throw, and catch keywords for exception handling in C++ and how stack unwinding is related to exception handling.

Uploaded by

1032221672
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views17 pages

Stack Unwinding

The document discusses stack unwinding in C++. It explains that stack unwinding involves calling destructors when an exception is thrown to remove function entries from the call stack. It also covers try, throw, and catch keywords for exception handling in C++ and how stack unwinding is related to exception handling.

Uploaded by

1032221672
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

School of Computer Engineering and Technology

First Year B. Tech. Computer Science and Engineering


CET1069B: Object Oriented Programming using C++
A.Y. 2022-23 Division:04 Semester: II
Active Learning
Topic: Stack Unwinding in C++
Group Id: 14
1) 104060 Shivain Rajput
2) 104071 Pratham Shrivastava
3) 104099 Siddhi Salunkhe
4) 104056 Gaurav Mishra
5) 104042 Vaidant Jain
Points to be Covered

▪ Introduction to Stack Unwinding


▪ Basics of Stack
▪ Concept of exception handling
▪ TRY, THROW and CATCH mechanism in C++
▪ Relation between Stack Unwinding and
Exception Handling
Introduction To Stack Unwinding

Stack Unwinding is basically a combination of two concepts


in C++ i.e., Stack and Exception Handling. It follows the
conventional exception handling mechanism but in this
destructor calling takes place just like POP function in
stack.

Prepared and Presented By


104099_Siddhi Salunkhe
What is Stack ???

Stack is a linear data structure that follows a particular


order in which the operations are performed. The order may
be LIFO(Last In First Out) || FILO(First In Last Out). LIFO
implies that the element that is inserted last, comes out first
and FILO implies that the element that is inserted first,
comes out last.

Prepared and Presented By


104099_Siddhi Salunkhe
Prepared and Presented By
104056_Gaurav Mishra
Push & Pop
Push:
Adds an item to the stack. If the stack is full, then it is said to be an
overflow condition.

Prepared and Presented By


104056_Gaurav Mishra
Pop:
Removes an item from the stack. The items
are popped in the reversed order in which
they are pushed. If the stack is empty,
then it is said to be an Underflow condition.

The element last to go in the stack pops out


first.

Prepared and Presented By


104056_Gaurav Mishra
Concept of exception handling
Starting with what are exceptions first of all:-
● When an error occurs, C++ will normally stop and generate an
error message. The technical term for this is: C++ will throw
an exception (throw an error). These are basically runtime
anomalies or abnormal conditions that a program encounters
during its execution.
● There are two types of exceptions: a)Synchronous,
b)Asynchronous (i.e., exceptions which are beyond the
program’s control, such as disc failure,etc). Prepared and Presented By
104071_Pratham Shrivastava
Concept of exception handling
● Now, exception handling is the process of responding to unwanted or
unexpected events when a computer program runs. Exception handling
deals with these events to avoid the program or system crashing, and
without this process, exceptions would disrupt the normal operation of
a program.

● Exception handling differs from error handling in that the former


involves conditions an application might catch versus serious problems
an application might want to avoid. In contrast, error handling helps
maintain the normal flow of software program execution.
Prepared and Presented By
104071_Pratham Shrivastava
Advantages of exception handling
1. Separation of Error Handling code from Normal Code:- In traditional error
handling codes, there are always if-else conditions to handle errors. This makes the
code less readable and maintainable.With try/catch blocks, the code for error
handling becomes separate from the normal flow

2. Functions/Methods can handle only the exceptions they choose:- A function can
throw many exceptions, but may choose to handle some of them. The exceptions
which are thrown but not caught, can be handled by the caller. If the caller chooses
not to catch them, then the exceptions are handled by the caller of the caller.

3. Grouping of Error Types:- In C++, both basic types and objects can be thrown as
exceptions. We can create a hierarchy of exception objects, group exceptions in
namespaces or classes and categorize them according to their types.
Prepared and Presented By
104071_Pratham Shrivastava
TRY,THROW and CATCH
mechanism in C++
Exception handling in C++ consists of three keywords: try, throw
and catch
1. The try statement allows you to define a block of code to be
tested for errors while it is being executed.
2. The throw keyword throws an exception when a problem is
detected, which lets us create a custom error.
3. The catch statement allows you to define a block of code to be
executed if an error occurs in the try block.
Prepared and Presented By
104060_Shivain Rajput
The try and catch keywords come in pairs :

● We use the try block to test some code: If the value of a variable “age” is less than 18, we will
throw an exception, and handle it in our catch block.
● In the catch block, we catch the error if it occurs and do something about it. The catch
statement takes a single parameter. So, if the value of age is 15 and that’s why we are throwing an
exception of type int in the try block (age), we can pass “int myNum” as the parameter to the
catch statement, where the variable “myNum” is used to output the value of age.
● If no error occurs (e.g. if age is 20 instead of 15, meaning it will be greater than 18), the catch
block is skipped.

output of the above code :-

Prepared and Presented By


104060_Shivain Rajput
There is a special catch block called the ‘catch all’ block, written as
catch(…), that can be used to catch all types of exceptions. For
example, in the following program, an int is thrown as an exception,
but there is no catch block for int, so the catch(…) block will be
executed.

Implicit type conversion doesn’t


happen for primitive types. For
example, in the following
program, ‘a’ is not implicitly
converted to int. Prepared and Presented By
104060_Shivain Rajput
Stack Unwinding && Exception
Handling
● Stack Unwinding is the process of removing function entries
from function call stack at run time. The local objects are
destroyed in reverse order in which they were constructed.
● In C++, when an exception occurs, the function call stack is
linearly searched for the exception handler, and all the entries
before the function with exception handler are removed from the
function call stack. So, stack unwinding is a process of calling the
destructors (whenever an exception is thrown) for all the
automatic objects constructed at run time. Prepared and Presented By
104042_Vaidant Jain
Continued…
In this, f1 function
throws an exception
and f3 catches it
and the block of
code after the
throw command will
not be executed.

Prepared and Presented By


104042_Vaidant Jain
References

▪ https://www.geeksforgeeks.org/exception-handling-c/
▪ https://www.w3schools.com/cpp/cpp_exceptions.asp
▪ https://www.tutorialspoint.com/cplusplus/cpp_exceptions_handling.htm
▪ https://www.techtarget.com/searchsoftwarequality/definition/error-handli
▪ https://learn.microsoft.com/en-us/cpp/cpp/exceptions-and-stack-unwindin
g-in-cpp?view=msvc-170
▪ https://www.geeksforgeeks.org/stack-unwinding-in-c/
Prepared and Presented By
104042_Vaidant Jain

You might also like