Handling Exceptions
In C++ programming, errors can occur during the execution of code, and it's essential to handle these errors gracefully. These errors may result from incorrect input or other unforeseen circumstances. Proper error handling is crucial to ensure the program's robustness and to provide informative feedback to users when errors occur. These error situations are commonly referred to as exceptions in C++.
- In try block, we write our code.
- If there is a problem in code, the throw keyword throws exception.
- The catch block has the code which is to be executed in case of error.
Let us explore an example.
In this example, our variable ph which takes input phone number in integers but if user input alphabet, it will throw an error.
Another Example
Practice Exercises
Complete these exercises to reinforce your learning and earn XP
Sign in to track your progress and earn XP!
Exercise 1 of 2Easy
Which of the following is a best practice in programming?
10 XP~2 min
Exercise 2 of 2Easy
Code that is easy to read and understand is called ___ code.
10 XP~2 min