StudyLover
  • Home
  • Study Zone
  • Profiles
  • Contact us
  • Sign in
StudyLover 26. Program to implement exception handling with & without the functionality of testing throw restrictions.
Download
  1. C++ Programs list
25. Program to search for a specific word in a file and count its occurrences. : 27. Program to use pointers for both base and derived classes and call the member functions using the `virtual` keyword.
C++ Programs list
  1.  Program to implement exception handling with the functionality of testing throw restrictions.

  1. #include <iostream.h>

  2. #include<conio.h>

  3. void divide(int a, int b) {

  4.     if (b == 0) {

  5.         throw "Division by zero error!";

  6.     } else {

  7.         cout << "Result: " << a / b << endl;

  8.     }

  9. }

  10.  

  11. int main() {

  12.     int x, y;

  13.  

  14.     cout << "Enter two numbers: ";

  15.     cin >> x >> y;

  16.  

  17.     try {

  18.         divide(x, y);

  19.     } catch (const char* msg) {

  20.         cerr << "Exception caught: " << msg << endl;

  21.     }

  22.     getch();

  23.     return 0;

  24. }

    Program to implement exception handling without the functionality of testing throw restrictions.

  1. #include <iostream.h>

  2. #include<conio.h>

  3. int divide(int a, int b) {

  4.     if (b == 0) {

  5.         return -1; // Error code for division by zero

  6.     }

  7.     return a / b;

  8. }

  9.  

  10. int main() {

  11.  

  12.  

  13.     cout<< "Enter a Divident: ";

  14.     int divident;

  15.     cin >> divident;

  16.     cout<< "Enter a divisor: ";

  17.     int divisor;

  18.     cin >> divisor;

  19.     int result = divide(divident, divisor);

  20.     if (result == -1) {

  21.         cout << "Error: Division by zero!" << endl;

  22.     } else {

  23.         cout << "Result: " << result << endl;

  24.     }

  25.  

  26.    getch();

  27.     return 0;

  28. }

25. Program to search for a specific word in a file and count its occurrences. 27. Program to use pointers for both base and derived classes and call the member functions using the `virtual` keyword.
Our Products & Services
  • Home
Connect with us
  • Contact us
  • +91 82955 87844
  • Rk6yadav@gmail.com

StudyLover - About us

The Best knowledge for Best people.

Copyright © StudyLover
Powered by Odoo - Create a free website