Site icon DataFlair

C++ Exception Handling

Master C++ with Real-time Projects and Kickstart Your Career Start Now!!

Program 1

#include <iostream>
using namespace std;

int main()
{
    system("cls");
    int a,b,c;
    cout<<"Enter two number for division: "<<endl;
    cin>>a>>b;
    try
    {
       if(b==0) 
         throw("can not divide by zero.....");
        c=a/b;    
       cout<<"Ans of (a/b) is : "<<c<<endl;
    } 
    catch( const char *str)
    {
        cout<<str<<endl;
    }
    cout<<"Program ends here"<<endl;
    cout<<"Ok bye ! visit again"<<endl;
    return 0;
}

Program 2

//File Opening Exception (Simple Simulation)
#include <iostream>
#include <fstream>
using namespace std;
int main() 
{
    system("cls");
    ifstream fin;
    fin.open("data.txt");

    try {
        if (!fin)
            throw "File could not be opened!";
        cout << "File opened successfully.\n";
    }
    catch (const char* msg) {
        cout << " Exception: " << msg << endl;
    }

    return 0;
}



// //Exception for Voting Age

// #include <iostream>
// using namespace std;

// void checkVotingAge(int age) 
// {
//     if (age < 18)
//         throw age;  // throw the age as an exception
//     cout << " You are eligible to vote.\n";
// }

// int main() 
// {
//     system("cls");
//     int age;
//     cout << "Enter your age: ";
//     cin >> age;

//     try 
//     {
//         checkVotingAge(age);
//     }
//     catch (int age) 
//     {
//         cout << " You are not eligible to vote. Age = " << age << endl;
//     }

//     return 0;
// }


// //Multiple catch Blocks
// #include <iostream>
// using namespace std;

// int main() 
// {
//     system("cls");
//     int a, b;
//     cout << "Enter two numbers: ";
//     cin >> a >> b;

//     try 
//     {
//         if (b == 0)
//             throw 0;
//         else if (b < 0)
//             throw string("Negative value not allowed");
//         cout << "Result = " << a / b;
//     }
//     catch (int) 
//     {
//         cout << " Error: Division by zero!";
//     }
//     catch (string s) 
//     {
//         cout << " Error: " << s;
//     }

//     return 0;
// }




//Array Index Out of Bounds
// #include <iostream>
// using namespace std;

// int main() 
// {
//      system("cls");
//     int arr[3] = {10, 20, 30};
//     int index;

//     cout << "Enter index (0-2): ";
//     cin >> index;

//     try {
//         if (index < 0 || index > 2)
//             throw index;
//         cout << "Value = " << arr[index];
//     }
//     catch (int i) 
//     {
//         cout << " Error: Invalid index " << i << "!";
//     }

//     return 0;
// }












// int main() 
// {
//     system("cls");
//     int a, b;
//     cout << "Enter two numbers (a / b): ";
//     cin >> a >> b;

//     try 
//     {
//         if (b == 0)
//             throw "Division by zero not allowed!";
//         cout << "Result = " << (a / b);
//     }
//     catch (const char* msg)
//      {
//         cout << " Error: " << msg;
//     }

//     return 0;
// }

 

Exit mobile version