Stack Unwinding
Stack Unwinding
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.
▪ 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