Exception Handling using classes in C++
- 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:
- 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.
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up