Simple Program for Exception Handling with Multiple Catch Using C++ Programming
Definition
To perform exception handling with multiple catch.
Exception Handling with Multiple Catch Algorithm/Steps:
- Step 1: Start the program.
- Step 2: Declare and define the function test().
- Step 3: Within the try block check whether the value is greater than zero or not.
- a. if the value greater than zero throw the value and catch the corresponding exception.
- b. Otherwise throw the character and catch the corresponding exception.
- Step 4: Read the integer and character values for the function test().
- Step 5: Stop the program.
Exception Handling with Multiple Catch Example Program
#include<iostream.h>
#include<conio.h>
void test(int x) {
try {
if (x > 0)
throw x;
else
throw 'x';
} catch (int x) {
cout << "Catch a integer and that integer is:" << x;
} catch (char x) {
cout << "Catch a character and that character is:" << x;
}
}
void main() {
clrscr();
cout << "Testing multiple catches\n:";
test(10);
test(0);
getch();
}
Sample Output
Testing multiple catches
Catch a integer and that integer is: 10
Catch a character and that character is: x
Exception Handling 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 Program for Function Overloading Using C++ Programming
- Simple Example Program For Copy Constructor In C++
- Simple Program for Single Inheritance Using C++ Programming
- Simple Program for Inline Function without Class Using C++ Programming
- Factorial Using Function Example Program In C++
- Simple Addition ( Add Two Integers ) Example Program
- Simple Example Program For Constructor In C++
- Simple Example Program for Inline Function Using C++ Programming
- Simple Example Program For Constructor Overloading In C++
- Simple Program for Read user Input Using cin
- 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 Copy Constructor Example Program For Find Factorial In C++
- Simple Program for Exception Handling Divide by zero Using C++ Programming