c + + exception handling

Original text: https://www.runoob.com/cplusplus/cpp-exceptions-handling.html
C + + exception handling
Exceptions are problems that occur during program execution. C + + exceptions are special cases that occur when a program is running, such as an attempt to divide by zero.

Exceptions provide a way to transfer control of the program. C + + exception handling involves three keywords: try, catch and throw.

Throw: when a problem occurs, the program throws an exception. This is done by using the throw keyword.
Catch: catch exceptions through the exception handler where you want to handle the problem. The catch keyword is used to catch exceptions.
Try: the code in the try block identifies the specific exception that will be activated. It is usually followed by one or more catch blocks.
If a block throws an exception, the method that catches the exception will use the try and catch keywords. The code that may throw an exception is placed in the try block. The code in the try block is called protection code. The syntax for using the try/catch statement is as follows:

try
{
   // Protection code
}catch( ExceptionName e1 )
{
   // catch block
}catch( ExceptionName e2 )
{
   // catch block
}catch( ExceptionName eN )
{
   // catch block
}

If the try block throws different exceptions in different situations, you can try to list multiple catch statements to catch different types of exceptions.
Throw exception
You can use the throw statement to throw an exception anywhere in the code block. The operand of the throw statement can be any expression, and the type of the result of the expression determines the type of exception thrown.

The following is an example of an exception thrown when trying to divide by zero:

double division(int a, int b)
{
   if( b == 0 )
   {
      throw "Division by zero condition!";
   }
   return (a/b);
}

Catch exception
The catch block follows the try block to catch exceptions. You can specify the type of exception you want to catch, which is determined by the exception declaration in parentheses after the catch keyword.

try
{
   // Protection code
}catch( ExceptionName e )
{
  // Code to handle exception name
}

The above code will catch an exception of type ExceptionName. If you want the catch block to handle any type of exception thrown by the try block, you must use an ellipsis... Within the parentheses of the exception declaration, as follows:

try
{
   // Protection code
}catch(...)
{
  // Code that can handle any exception
}

The following is an example that throws an exception divided by zero and catches the exception in the catch block.
example

#include <iostream>
using namespace std;
 
double division(int a, int b)
{
   if( b == 0 )
   {
      throw "Division by zero condition!";
   }
   return (a/b);
}
 
int main ()
{
   int x = 50;
   int y = 0;
   double z = 0;
 
   try {
     z = division(x, y);
     cout << z << endl;
   }catch (const char* msg) {
     cerr << msg << endl;
   }
 
   return 0;
}

Since we threw an exception of type const char *, we must use const char * in the catch block when we catch the exception. When the above code is compiled and executed, it will produce the following results:

Division by zero condition!

Exceptions to the C + + standard

C + + provides a series of standard exceptions, which are defined in. We can use these standard exceptions in programs. They are organized in a parent-child hierarchy as follows:

Define a new exception

You can define new exceptions by inheriting and overloading the exception class. The following example demonstrates how to use the std::exception class to implement your own exception:

#include <iostream>
#include <exception>
using namespace std;
 
struct MyException : public exception
{
  const char * what () const throw ()
  {
    return "C++ Exception";
  }
};
 
int main()
{
  try
  {
    throw MyException();
  }
  catch(MyException& e)
  {
    std::cout << "MyException caught" << std::endl;
    std::cout << e.what() << std::endl;
  }
  catch(std::exception& e)
  {
    //Other errors
  }
}

This will produce the following results:

MyException caught
C++ Exception

Here, what() is a public method provided by the exception class, which has been overloaded by all child exception classes. This returns the cause of the exception.

Tags: C++ C# html

Posted on Tue, 14 Sep 2021 19:13:07 -0400 by anthill