Exception Handling In C++
Exceptions Handling Definition:
In C++ Programming, Exception Handling is the best method for handles run-time errors. It provides a method to convey control from try block of a program to catch block. C++ implements following three specific keywords for exception handling: try, catch, and throw.
try block:
- The try keyword is used to declare a block of statements, which code may produce exceptions.
- When an exception is identified, It is thrown using a throw statement in the try Block. program control immediately passes to catch block from the try block.
a try block syntax looks like the following:
try {
//main code
}
Catch block:
The catch block represented by the keyword catch. Catch block catches exception the exception thrown by the throw statement in the try block and catch block handles the exception.
a catch block syntax looks like the following:
try {
} catch (ExceptionType1 name) {
//exception code
} catch (ExceptionType2 name) {
//exception code
}
throw Statement:
Possible to declare throw keyword anywhere in the try block. throw keyword trigger/execute a matched type of the catch block.The try block exists When a throw statement is reached.
a throw statement syntax looks like the following:
try {
throw data_value;
} catch (ExceptionType1 name) {
//exception code
} catch (ExceptionType2 name) {
//exception code
}
Exception Handling Checklist
- Try block has the main code, which code may throw an exception.
- Multiple throws are possible to declare in try block based different situations.
- The catch block contains exception handling code that is executed when thrown by the Try Block.
- Possible to declare one or more catch block for different type of exception
- No code can be between try block and the first catch block.
- Catch blocks declare directly after the try block.
Simple Exception Program In C++
// Simple Exception Program In C++
#include <iostream>
using namespace std;
int main() {
//try block
try {
// Exception throwed
throw 100;
} //catch block
catch (int exception_value) {
cout << "Exception Occurred : Exception Value : " << exception_value;
}
return 0;
}
Exception Occurred : Exception Value : 100
Multiple Catch Block Exception Program In C++
// Multiple Catch Exception Example Program (Divide By Zero ) In C++
#include <iostream>
using namespace std;
float divide(int x, int y) {
if (y == 0) {
throw y;
} else if (y < 0) {
throw "Negative Input";
}
return (x / y);
}
int main() {
int i, result;
//try block
cout << "Enter the Number :";
cin>>i;
try {
result = divide(100, i);
cout << result << endl;
} //catch block
catch (int exception_value) {
cout << "Exception Occurred : Exception Value : " << exception_value;
} catch (const char* excpection_str) {
cout << "Exception Occurred : Exception Value : " << excpection_str;
}
return 0;
}
/*
Execution Scenarios
// Scenario 1
result = divide(100, 10);
cout << result << endl;
// Scenario 2
result = divide(100, 0);
cout << result << endl;
// Scenario 3
result = divide(100, -1);
cout << result << endl;
*/
Sample Output:
Scenario 1:
Enter the Number :10
10
Scenario 2:
Enter the Number :0
Exception Occurred : Exception Value: 0
Scenario 3:
Enter the Number :-1
Exception Occurred : Exception Value: Negative Input
Exception Handling Example Programs In C++
- Simple C++ Program for Basic Exception Handling
- Simple Program for Exception Handling Divide by zero Using C++ Programming
- Simple Program for Exception Handling with Multiple Catch Using C++ Programming
- Simple C++ Program for Catch All or Default Exception Handling
- Simple C++ Program for Rethrowing Exception Handling in Function
- Simple C++ Program for Nested Exception Handling
Read More Articles
- Simple Merge Sort Program in C++
- Scope Resolution Operator In C++
- Simple Program for Virtual Functions Using C++ Programming
- Simple Class Example Program For Find Prime Number In C++
- Simple Example Program For Parameterized Constructor In C++
- Define Constructor in Outside Class Example Program In C++
- Simple Example Program For Copy Constructor In C++
- Simple Program for Function Overloading Using C++ Programming
- Simple Program for Single Inheritance Using C++ Programming
- Simple Program for Inline Function without Class Using C++ Programming
- Simple Addition ( Add Two Integers ) Example Program
- Simple Example Program For Constructor In C++
- Factorial Using Function Example Program In C++
- Simple Example Program for Inline Function Using C++ Programming
- Simple Program for Read user Input Using cin
- Simple Example Program For Constructor Overloading In C++
- Factorial Using Loop Example Program In C++
- Simple Stack Program in C++ Programming
- Simple Program for Friend Function Using C++ Programming
- Simple Program for Static Data and Member Function Using C++ Programming
- Simple Program for Unary Operator Overloading Using C++ Programming
- Do While Loop Example Program In C++
- Simple Program for Multiple Inheritance Using C++ Programming
- Simple Program for Binary Operator Overloading Using C++ Programming
- Simple Copy Constructor Example Program For Find Factorial In C++