Exception Handling using classes in C++
In this article, we will discuss how to handle the exceptions using classes.
- Exceptions are run-time anomalies or abnormal conditions that a program encounters during its execution.
- There are two types of exceptions:
- Synchronous Exception
- Asynchronous Exception(Ex: which are beyond the program’s control, Disc failure, etc).
- C++ provides the following specialized keywords for this purpose:
- Create a class Numbers which has two data members a and b.
- Write iterative functions to find the GCD of two numbers.
- Write an iterative function to check whether any given number is prime or not. If found to be true, then throws an exception to class MyPrimeException.
- Define your own MyPrimeException class.
- Define a class named Number which has two private data members as a and b.
- Define two member functions as:
- int gcd(): to calculate the HCF of the two numbers.
- bool isPrime(): to check if the given number is prime or not.
- Use constructor which is used to initialize the data members.
- Take another class named Temporary which will be called when an exception is thrown.
Below is the implementation to illustrate the concept of Exception Handling using classes:
GCD is = 1 13 is a prime number Caught exception of MyPrimeException class.
My Personal Notes arrow_drop_up
Please Login to comment...