Hi there, we’re Harisystems
"Unlock your potential and soar to new heights with our exclusive online courses! Ignite your passion, acquire valuable skills, and embrace limitless possibilities. Don't miss out on our limited-time sale - invest in yourself today and embark on a journey of personal and professional growth. Enroll now and shape your future with knowledge that lasts a lifetime!".
For corporate trainings, projects, and real world experience reach us. We believe that education should be accessible to all, regardless of geographical location or background.
1C++ Error Handling
Error handling is an essential aspect of programming to handle unexpected situations and ensure the stability and reliability of your code. In C++, there are several mechanisms available for error handling. In this article, we will explore the usage of error handling techniques in C++ with examples.
1. Exceptions
C++ provides exception handling, which allows you to catch and handle exceptional conditions that may occur during the execution of your program. Exceptions provide a structured way to handle errors and propagate them up the call stack until they are caught. Here's an example:
#include <iostream>
#include <stdexcept>
double divide(double numerator, double denominator) {
if (denominator == 0) {
throw std::runtime_error("Divide by zero exception");
}
return numerator / denominator;
}
int main() {
try {
double result = divide(10, 0);
std::cout << "Result: " << result << std::endl;
} catch (const std::runtime_error& e) {
std::cout << "Exception caught: " << e.what() << std::endl;
}
return 0;
}
In the above code, we define a function divide()
that takes two double values and performs division. If the denominator is zero, we throw a std::runtime_error
exception with an appropriate error message. In the main()
function, we call the divide()
function inside a try
block. If an exception is thrown, it is caught by the catch
block, where we can handle the exception and display an error message.
2. Return Codes
C++ programs can use return codes to indicate success or failure. Conventionally, a return value of 0
is considered a successful execution, while non-zero values indicate errors or specific conditions. Here's an example:
#include <iostream>
int divide(int numerator, int denominator, int& result) {
if (denominator == 0) {
return -1;
}
result = numerator / denominator;
return 0;
}
int main() {
int result;
int status = divide(10, 0, result);
if (status == 0) {
std::cout << "Result: " << result << std::endl;
} else {
std::cout << "Error: Divide by zero" << std::endl;
}
return 0;
}
In the above code, we define a function divide()
that takes two integer values and performs division. If the denominator is zero, we return a non-zero value (-1) to indicate an error. Otherwise, we calculate the result and return 0
to indicate success. In the main()
function, we call the divide()
function and check the return value to determine if the operation was successful or not.
3. Assertion
C++ provides the assert()
macro, which allows you to verify assumptions in your code. If the condition passed to assert()
evaluates to false, it triggers an assertion failure and terminates the program. Here's an example:
#include <iostream>
#include <cassert>
int divide(int numerator, int denominator) {
assert(denominator != 0);
return numerator / denominator;
}
int main() {
int result = divide(10, 0);
std::cout << "Result: " << result << std::endl;
return 0;
}
In the above code, we define a function divide()
that performs division. We use the assert()
macro to check if the denominator is zero. If the condition evaluates to false, the program terminates and displays an error message indicating the assertion failure. Assertions are mainly used during development and debugging to catch programming errors and ensure assumptions are met.
C++ provides various error handling mechanisms to deal with exceptional situations and ensure the robustness of your code. Utilize exceptions, return codes, and assertions based on the specific needs of your program to handle errors and unexpected conditions effectively.
4.5L
Learners
20+
Instructors
50+
Courses
6.0L
Course enrollments
Future Trending Courses
When selecting, a course, Here are a few areas that are expected to be in demand in the future:.
Future Learning for all
If you’re passionate and ready to dive in, we’d love to join 1:1 classes for you. We’re committed to support our learners and professionals their development and well-being.
View CoursesMost Popular Course topics
These are the most popular course topics among Software Courses for learners