Try Catch C ++ exception caught

A Exception Handling

Abnormal capture is one of the conventional methods we used in the process of doing the project, there has been very good to explore many bloggers, as follows

https://blog.csdn.net/weixin_34291004/article/details/92389603

https://www.cnblogs.com/MrYuan/p/4800257.html

 

II. Discussion focused on (a try catch problem encountered failure in the process of writing the demo)

. 1  the try 
2      {
 . 3          char * PCH;
 . 4          PCH = ( char *) 00,001,234 ;    // administering to an illegal address 
. 5          * = PCH . 6 ; // illegal address assignment, causes Access Violation exception 
. 6          STD :: COUT << " ! IT IS the OK " << STD :: endl;
 . 7      }
 . 8      the catch ( const STD :: & Exception )
 . 9      {
 10          STD :: cerr, << " catched " << std::endl;
11     }
View Code

The code can not catch the exception,

The following are possible

. 1      the try 
2      {
 . 3          char * PCH;
 . 4          PCH = ( char *) 00,001,234 ;    // administering to an illegal address 
. 5          * = PCH . 6 ; // illegal address assignment, causes Access Violation exception 
. 6          STD :: COUT << " ! IT IS the OK " << STD :: endl;
 . 7      }
 . 8      the catch (...)
 . 9      {
 10          STD :: cerr, << " catched " << STD :: endl;
 . 11      }
View Code

The difference between the two codes is, catch (const std :: exception &) and catch (...)

catch (...) can capture the exception object multiple data types, it provides the programmer with a kind of abnormal objects better means of control, the software system development with good reliability. Therefore, a more experienced programmers often organized as code modules written in its

 

. 1  void Func ()
 2  {
 . 3    the try 
. 4    {
 . 5      // herein program code completion really complicated calculations, during execution code
 6      // may throw objects DataType1, DataType2 and DataType3 type. 
. 7    }
 . 8    the catch (DataType1 & D1)
 . 9    {
 10    }
 . 11    the catch (DataType2 & D2)
 12 is    {
 13 is    }
 14    the catch (DataType3 & D3)
 15    {
 16    }
 . 17    / * *************** *****************************************
18    Note that the above might be thrown in the try block DataType1, DataType2 and DataType3 three
 . 19    types of objects in front of the exception have a corresponding catch block to process. But why
 20    but also in the final and then define a catch (...) block it? This is in order to have better security and
 21    caused when the reliability of the process, to avoid the above try block throws another exception object does not take into account the
 22    serious consequences unexpected collapse sequence occurs, and this in a system developed by VC a more particularly effective, because
 23    is a catch (...) can capture the system is abnormal, and abnormal system programmers often make headache, now
 24    are generally more complex the system, but also by a lot of people to develop, accidentally resulting in a
 25    pointer variable points to other illegal areas, the results of the accident disaster misfortune happened. catch (...) such as
 26    to provide an effective remedy potential pitfalls.
27    ************************************************* ******* * / 
28  
29    the catch (...)
 30    {
 31 is    }
 32 }

 

Guess you like

Origin www.cnblogs.com/0523jy/p/11367831.html