C++ C++ Tutorial C++ Functions C++ Classes



CPP Exceptions

C++ is a powerful programming language that allows developers to create complex applications. However, with great power comes great responsibility. One of the biggest challenges in programming is handling errors and exceptions. C++ provides a robust mechanism for handling exceptions, which is known as C++ exceptions.

C++ exceptions are a way to handle errors and exceptional situations in a program. An exception is an event that occurs during the execution of a program that disrupts the normal flow of the program's instructions. When an exception occurs, the program stops executing and jumps to a special block of code called an exception handler. The exception handler is responsible for handling the exception and taking appropriate action.

The basic syntax for throwing an exception in C++ is as follows:


throw exception_type;

The exception_type can be any data type, including built-in types, user-defined types, or even pointers. When an exception is thrown, the program searches for an exception handler that can handle the exception. If no handler is found, the program terminates.

Here is an example of how to use C++ exceptions:


#include <iostream>
using namespace std;

int main() {
   try {
      int x = 10;
      int y = 0;
      if (y == 0) {
         throw "Division by zero";
      }
      int z = x / y;
      cout << "Result: " << z << endl;
   }
   catch (const char* msg) {
      cerr << "Error: " << msg << endl;
   }
   return 0;
}

In this example, we are trying to divide a number by zero, which is not allowed. Therefore, we throw an exception with the message "Division by zero". We then catch the exception using a catch block and print the error message to the console.

C++ exceptions can also be used to handle errors in functions. Here is an example:


#include <iostream>
using namespace std;

double divide(double x, double y) {
   if (y == 0) {
      throw "Division by zero";
   }
   return x / y;
}

int main() {
   try {
      double x = 10;
      double y = 0;
      double z = divide(x, y);
      cout << "Result: " << z << endl;
   }
   catch (const char* msg) {
      cerr << "Error: " << msg << endl;
   }
   return 0;
}

In this example, we define a function called divide that takes two arguments and returns their quotient. If the second argument is zero, we throw an exception with the message "Division by zero". We then call the divide function in the main function and catch any exceptions that are thrown.

C++ exceptions can also be used to handle errors in object-oriented programming. Here is an example:


#include <iostream>
using namespace std;

class DivideByZeroException {
public:
   const char* what() const throw() {
      return "Division by zero";
   }
};

class Calculator {
public:
   double divide(double x, double y) {
      if (y == 0) {
         throw DivideByZeroException();
      }
      return x / y;
   }
};

int main() {
   try {
      Calculator calc;
      double x = 10;
      double y = 0;
      double z = calc.divide(x, y);
      cout << "Result: " << z << endl;
   }
   catch (const DivideByZeroException& e) {
      cerr << "Error: " << e.what() << endl;
   }
   return 0;
}

In this example, we define a class called DivideByZeroException that inherits from the std::exception class. We then define a Calculator class that has a divide method that throws a DivideByZeroException if the second argument is zero. We then create an instance of the Calculator class in the main function and catch any exceptions that are thrown.

C++ exceptions are a powerful mechanism for handling errors and exceptional situations in a program. They allow developers to write robust and reliable code that can handle unexpected situations. By using C++ exceptions, developers can create applications that are more resilient and less prone to crashes and errors.

References

Activity