Types of Execution in C++
Introduction:
An exception in C++ is a program executing occurrence that interferes with the regular flow of instruction. Exceptions are employed to elegantly manage mistakes and unusual circumstances. Software may generate an exception to indicate that an unanticipated thing occurred when an extraordinary circumstance occurs. The relevant exception-handling code can then catch and manage the throw of the occurrence.
Exception handling is one of the reasons C++ is superior to C. Runtime abnormalities or unusual circumstances that an application runs across are known as exceptions. Two categories of exceptions exist:
1) Synchronous
2) Asynchronous.
For this reason, C++ offers the following specific keywords:
- Try this: indicates a code block with the ability to generate exceptions.
- Catch: Denotes a sequence of instructions in response to a specific exception being generated.
- Throw A command for tossing a thrown exception. Employed to list errors that a given function raises but cannot deal with.
A brief explanation of how C++ exceptions operate:
- Making Exceptions:
Employ the throw keywords and a phrase to throw an error. Any information type, including foundational types, points, objects, etc., may be used in this statement.
- Seizing Exceptions:
Use an attempt block, a few catch blocks, and then another try frame to catch an exception. The kinds of exceptions the detection blocks can handle are indicated.
- Hierarchy for Exception Handling:
A hierarchical structure of exception classes descended from std::exception is provided by C++. This order of priority can be used to catch exceptions, enabling you to detect general problems preceding more particular ones.
- Lastly, clean up (using RAII):
Resources Acquiring Is Initializing (RAII) concepts are commonly used in C++ for handling resources and cleaning.
- By utilizing variations, you can improve the readability and maintainability of your code by separating the handling errors logic from the application's regular flow. Nonetheless, it's crucial to utilize exceptions sparingly and avoid depending on them for regular application functionality. Exemptions ought to be kept for particular reasons.
C++ exception types include:
- Std::exception:
The fundamental class of all standard C++ exceptions. It offers a function called what() that yields a string describing the error.
- std::bad_alloc:
issued by new if a memory distribution demand is not fulfilled throughout the memory distribution process.
- std::bad_cast:
issued by dynamic_cast when an invalid gloom occurs for the type of item from which a pointer or reference is cast.
- std::bad_typeid
Whenever the type_info object cannot generate type data for an expression, typeid throws this exception.
- Std::logic_error:
Illustrates mistakes that arise from breaking an apparent condition. Out_of_range and domain_error are a pair of instances.
- std::runtime_error:
indicates mistakes that are only detectable during execution. Range_error and overflow_error are two examples.
- std::range_out
thrown whenever a request is made to get a component beyond the valid range via operations like vector::at().
- Overflow error (std::)
Generated by overflowing arithmetic operations.
- Underflow error (std::)
Thrown by underflow-causing arithmetic operations.
- std::inaccurate argument:
Thrown whenever a function receives an incorrect parameter.
- std::domain_error:
triggered whenever a domain error happens, like when an unfavourable number's square root is encountered.
- Std::length_error:
Raised when a particular operation (such as producing an array or array longer than permitted) would produce an improper length.
- std::range_error:
triggered when there isn't a mathematical conclusion that fits inside the permitted range.
- Failure of std::ios_base::
thrown to indicate faults by input and output library objects.
Example Code:
#include <iostream>
using namespace std;
int main()
{
int y = -1;
cout << "Before the try \n";
try {
cout << "Inside try \n";
if (y < 0)
{
throw y;
cout << "After the throw (Not executed) \n";
}
}
catch (int y ) {
cout << "Exception Caught \n";
}
cout << "After catch (Will execute) \n";
return 0;
}
Output:
![Types of Execution in C++](https://static.tutorialandexample.com/cpp/types-of-execution-in-cpp1.png)
Example Code:
#include <iostream>
using namespace std;
int main()
{
try {
throw 1;
}
catch (char *excp) {
cout << "Caught " << excp;
}
catch (...) {
cout << "standard deviation\n";
}
return 0;
}
Output:
![Types of Execution in C++](https://static.tutorialandexample.com/cpp/types-of-execution-in-cpp2.png)
To capture all kinds of variations, a unique caught block is known as the "catch-all" block, expressed as catch(..). For instance, when an int is thrown as an exception in the program below, the catch(...) statement will be run as there isn't a catch stop for ints.
Example Code:
#include <iostream>
using namespace std;
int main()
{
try {
throw 'x';
}
catch (int p) {
cout << "Caught ";
}
return 0;
}
Output:
![Types of Execution in C++](https://static.tutorialandexample.com/cpp/types-of-execution-in-cpp3.png)
An abnormal program termination occurs if a conditional gets thrown and not captured. For instance, a char is thrown in the program below, but no catch block is there to capture the char.