C Exception Handling

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_24624539/article/details/90597979

C ++ exception handling

An exception is the issue procedurally generated during execution. C ++ exception refers to the special circumstances of the program is running, such as the attempt to divide by zero operation.

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

  • throw:  When a problem occurs, the program will throw an exception. This is done by using the  throw  keyword to complete.
  • catch:  where you want to deal with the problem, catch the exception by exception handler. catch  keyword is used to catch the exception.
  • the try:  the try  block identification codes to be activated a certain abnormality. It is usually followed by one or more catch blocks.

If there is a block throws an exception, the exception will capture method using  try  and  catch  keywords. placed try block code may throw an exception, the try block code is referred to as protected codes. Use try / catch statement syntax is as follows:

try
{
   // 保护代码
}catch( ExceptionName e1 )
{
   // catch 块
}catch( ExceptionName e2 )
{
   // catch 块
}catch( ExceptionName eN )
{
   // catch 块
}

If the  try  block throws different exceptions in different contexts, this time to try to list multiple  catch  statements to catch different types of exceptions.

Throw an exception

You can use the  throw  statement to throw an exception anywhere in the code block. throw statement operands can be any expression type of the result of the expression determines the type of exception thrown.

The following are examples of attempts on division by zero throws an exception:

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

Catch the exception

catch  blocks follow the  try  back block, to catch exceptions. You can specify the type of exception you want to capture, which is determined by abnormal declared in parentheses after the catch keyword.

try {// protection code} catch (ExceptionName e) {// exception handling code} exceptionName

The above type of a code capture  ExceptionName  exception. If you want to catch block can handle any type of exception thrown in the try block, it must be used within parentheses exception declarations ... ellipsis, as follows:

try {// protection code} catch (...) {// code can handle any exceptions}

The following is an example of an exception is thrown a divide by zero, and capture the exception in the catch block.

Examples

 

#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 throw a type  const char *  exception, therefore, when catch the exception, we have to use const char * in the catch block. When the above code is compiled and executed, it produces the following results:

Division by zero condition!

C ++ standard exceptions

C ++ provides a set of standard exceptions are defined in the  <exception>  , we can use these standard exceptions in the program. They are based on parent-child class hierarchy organized as follows:

C ++ hierarchy abnormalities

The following table is a description of each of the above abnormal hierarchy appears:

abnormal description
std::exception The exceptions are all standard C ++ exceptions parent.
std::bad_alloc The exception can be  new  throw.
std::bad_cast The exception can be  dynamic_cast  throws.
std::bad_exception This is useful when dealing with C ++ program can not be expected exception.
std::bad_typeid The exception can be  typeid  throws.
std::logic_error Theoretically the abnormality can be detected by reading the code.
std::domain_error When using an invalid domain of mathematics, it will be thrown.
std::invalid_argument When using an invalid argument, it will be thrown.
std::length_error When creating too long std :: string, it will be thrown.
std::out_of_range The method throws an exception can be, for example, std :: vector and std :: bitset <> :: operator [] ().
std::runtime_error It is theoretically impossible to detect the abnormality by reading the code.
std::overflow_error When the spill occurred on mathematics, it will be thrown.
std::range_error When attempting to store values ​​out of range, it will be thrown.
std::underflow_error When underflow occurs mathematics, it will be thrown.

Define a new exception

You can inheritance and overloading  exception  to define a new exception class. The following example demonstrates how to use std :: exception class to implement their own exceptions:

Examples

#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)
  {
    //其他的错误
  }
}

This produces the following results:

MyException caught
C++ Exception

Here, What ()  is a common method of providing exception class, it has been overloaded all sub exception classes. This returns the cause of the anomaly produced.

Guess you like

Origin blog.csdn.net/qq_24624539/article/details/90597979