C++ Program for Exception Handling with Multiple Catch

0
122
C++ Program for Exception Handling with Multiple Catch

In this article will be writing a C++ program for Exception Handling with Multiple Catch Using C++ Programming

At first, Create a C++ program then Declare and define the function test(). Within the try, block check whether the value is greater than zero or not. if the value is greater than zero throws the value and catches the corresponding exception. Otherwise throw the character and catch the corresponding exception. Read the integer and character values for the function test().

 #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);
  
Testing multiple catches
Catch a integer and that integer is: 10
Catch a character and that character is: x

Introduction to C Programming Language

Previous articleIntroduction to C Programming Language
Next articleC++ Files and Streams

LEAVE A REPLY

Please enter your comment!
Please enter your name here