C + + exception handling

Source: Internet
Author: User

A C + + exception handling mechanism

Exception handling Basic idea: the process of executing a function found an exception, can not be processed immediately within the function, but to throw the exception, so that the function of the caller directly or indirectly deal with the problem.

C + + exception handling binary consists of three modules: try, throw, catch

  Try---Catch basic syntax:

Try     catch (type name [formal parameter name])//{//     catch(type name [formal parameter name])//{  //   catch(..... )//{  

Example:

voidF1 (inti) {    if(i<0)        Throw 1;}voidF2 () {F1 (- $);}voidF3 () {F2 ();}voidF4 () {F3 ();}intMain () {Try{f4 (); }    Catch(inti) {cout<<"Error code:"<< I <<"\ n"<<Endl; } System ("Pause"); return 0;}
Matching rules for two exception captures

What type of exception is thrown inside a try block, and in general, the type must match exactly, but the following three cases can be type-cast:

(1) Run from non-const object to const object conversion, that is, throw a non-const object, catch a const object

(2) running conversions from a derived class type to a base type

(3) Convert an array to a pointer to a group type, and convert the function to a pointer to a function type

voidFuninti) {    if(I <0)        Throw-1;}voidfun2 () {Try{Fun (- at); }    Catch(inti) { switch (i)
{
Case -1;
throw "data out of bounds"; Re-throw
} }}intMain () { try
{
Fun2 (-34);
}
catch (const char *s)
{
cout << "error code:" << s<<"\ n" << Endl;
}
System ("pause");
return 0; }
Three exception specification

(1) After the function declaration, list the type of exception that the function might throw, and ensure that the function does not throw other types of exceptions

A. void fun () throw (Toobig,toosmall,divzero); function throws a specified exception

B. void f (); This means that the function throws any kind of exception

C. void f () throw (); This means that the function does not throw an exception

(2) The guarantee does not throw an exception in the constructor, because the function of the constructor is to construct and initialize the object, and once an exception is thrown, it may cause the object to be incomplete or not fully initialized

(3) Guaranteed not to throw an exception in the destructor, because the function of the destructor is to complete the cleanup of resources, once thrown an exception, it is possible to cause memory leaks and so on

C + + exception handling

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.