Javatpoint Logo
Javatpoint Logo

C++ program to handle the checked exceptions

An exception is an unexcepted event that occurs during the execution of the program that stops the normal flow of the program. Two types of exceptions are checked and unchecked exceptions. Checked exceptions are compile-time exceptions because the compiler checks these exceptions during compile-time, whereas the compiler does not check unchecked exceptions.

In C++, there is no concept of checked exceptions like in other languages such as Java. In C++, exceptions are unchecked, which means you can throw any type of exception from any function, and you are not required to declare exceptions a function might throw. The programmer is responsible for handling exceptions. So, the programmer must use the try, catch, and throw keywords for handling expectations and throw the type of object causing exceptions using the try block.

Why are exceptions not checked by the CPP compiler?

C++ is an extended version of the C language, but C language does not have support for exceptions. So, if exceptions are implemented, they break the existing C program.

Checking exceptions at compile time will make the compiler analyse the flowcharts, increasing the computation and the runtime. This overhead is not applicable for performance-sensitive applications.

C++ allows programmers to choose when and where to handle exceptions. This flexibility makes the C++ uncheck the exceptions at the compile level.

Example 1:

Let us take an example to illustrate how to handle the checked exceptions.

Output:

C++ program to handle the checked exceptions

Explanation:

The variables present in the program are a and b, which denote the numerator and the denominator, which are taken from the user.

Division and main are the two functions in the program. The division function has two arguments, a and b. Its return type is int, which gives the division of a and b. This program uses the error handling concept of try, catch, and throw keywords. The try block contains the code that may give an error, and the catch block contains the error message. The throw keyword is used to throw the error message.

Example 2:

Let us take another example to illustrate how to handle the checked exceptions.

Output:

C++ program to handle the checked exceptions

Explanation:

In this example, the program contains several functions, including BankAccount() constructor, deposit(), withdraw(), transfer(), and getBalance() functions.

Here, the deposit() function takes the amount as input and checks whether the amount is negative or not. If negative, it will throw an invalid_argument exception. Otherwise, it increases the balance by the deposited amount.

The withdraw() function takes the amount as input. If it is negative, it throws an invalid_argument exception. Otherwise, it decreases the balance by the withdrawn amount. If the withdrawal amount exceeds the balance, it throws an insufficientFundsException message.

The transfer() function takes an amount and a reference to another BankAccount object as input. It tries to withdraw the specified amount from the current account. If successful, deposit the amount into the recipient's account. If an InsufficientFundsException is caught during the withdrawal, it handles the exception by printing an error message. It prints a message indicating the transfer operation.

Error handling concept:

  • In the transfer function, a try block attempts the withdrawal and transfer operation.
  • If an InsufficientFundsException is caught during the withdrawal, it is handled in the catch block, where an error message is printed.
  • In the main function, similar try-catch blocks are used to catch any exceptions that might be thrown during the operations.

Conclusion:

In conclusion, C++ offers powerful exception-handling mechanisms through try, catch, and throw, enabling developers to manage unexpected events gracefully. Unlike Java, C++ does not enforce checked exceptions, allowing programmers to handle exceptions based on their specific application requirements. Using try-catch blocks, C++ developers can safeguard their programs from runtime errors and maintain control over program flow. The examples demonstrate how exceptions can be thrown, caught, and handled, enhancing the reliability and robustness of C++ applications in the face of exceptional conditions. Understanding and applying exception-handling principles is crucial for creating stable, user-friendly software in C++.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA