C + + tips for throwing exceptions

Source: Internet
Author: User
Tags define exception define function throw exception

The C + + language, like other programming languages, also contains handling of exceptions. Here we will give you a detailed explanation of C + + throw exception implementation methods, and the application of exceptions. I hope that we can get some help to improve the understanding of this.

C + + throws an exception (also known as discarding an exception) that detects whether an exception is generated, in C + +, which is implemented by the throw statement, and throws an exception if an exception is detected. The format of the statement is:

throw-expression;

If an exception is found in the program segment of the TRY statement block, including the function in which it was called, and the exception is discarded, the exception can be caught and processed by a catch statement after the try statement block, and the condition that is caught and processed is that the type of the thrown exception matches the exception type of the catch statement. Because C + + uses data types to differentiate between different exceptions, the value of the expression in the throw statement has no practical significance when judging the exception, and the type of the expression is particularly important.

Handles an exception with a divisor of 0. The example uses the Try/catch statement to catch an exception with the above divisor of 0, and use the throw statement to implement C + + to throw an exception, implementing exception handling, as shown in the code listing.

#include//contains header file #include double fuc (double x, double y)//define function

{if (y==0)

{throw y;//divisor is 0, throw exception}

return x/y; Otherwise return two number of quotient}

void Main ()

{double res; try//define exception

{res=fuc (2,3); cout<< "The result of X/y is:" <<res<<endl; p= ""}<= "" exception = "" Res= "fuc (4,0);" >

catch (double)//catch and handle exceptions

{cerr<< "error of dividing zero.\n"; exit (1);//Exception Exit program}

}

"Run Results" create a new C + + source file in Visual C + +, enter the code above, and run it correctly after compilation.

"Sample parsing" in the preceding code, the try statement is used in the 14th to 19th line of the main function main () to define the exception, which contains 3 statements with possible exceptions, and they are functions that divide the two number of calls. In line 20th to 24th of the code, exception handling is defined, that is, the statement in the code is executed after the exception is caught. In addition, the Code 5~8 line in function fuc () implements C + + throwing exceptions through the throw statement.

http://biyinjishi.blog.163.com/blog/static/259102036201631810162802
http://biyinjishi.lofter.com/post/1de35feb_aaa4656
http://blog.sina.com.cn/s/blog_15e2eae990102waiq.html
http://blog.sina.com.cn/s/blog_15e2eae990102wair.html
http://blog.tianya.cn/post-7182283-114494321-1.shtml
http://blog.tianya.cn/post-7182283-114494120-1.shtml
http://blog.tianya.cn/post-7208879-114494822-1.shtml
http://blog.tianya.cn/post-7208879-114494804-1.shtml
http://blog.tianya.cn/post-7208879-114494833-1.shtml
http://blog.tianya.cn/post-7208879-114494791-1.shtml
http://blog.tianya.cn/post-7208879-114494780-1.shtml
http://blog.tianya.cn/post-7208879-114494771-1.shtml
http://blog.tianya.cn/post-7208879-114494730-1.shtml
http://blog.tianya.cn/post-7208879-114494711-1.shtml
http://blog.tianya.cn/post-7208879-114494692-1.shtml
http://blog.tianya.cn/post-7208879-114494661-1.shtml
http://tieba.baidu.com/p/4487929040
http://www.cnblogs.com/SA-Jim/p/5403744.html
http://www.cnblogs.com/SA-Jim/p/5403753.html
http://www.cnblogs.com/SA-Jim/p/5403764.html
http://www.cnblogs.com/SA-Jim/p/5403774.html
http://www.cnblogs.com/SA-Jim/p/5403778.html
http://www.cnblogs.com/SA-Jim/p/5403785.html
http://www.cnblogs.com/SA-Jim/p/5403789.html
http://www.cnblogs.com/SA-Jim/p/5403797.html
http://bbs.pcauto.com.cn/topic-10937713.html
http://baa.bitauto.com/bj/thread-9012204.html#131544312
http://baa.bitauto.com/bj/thread-9012274.html#131544963
http://bbs.pcauto.com.cn/topic-10937702.html
http://www.biyinjishi.com/kdoc/89456261/
http://www.biyinjishi.com/kdoc/89456267/
http://www.biyinjishi.com/kdoc/89456268/
http://www.biyinjishi.com/kdoc/89456258/
http://www.biyinjishi.com/kdoc/89456263/
http://www.biyinjishi.com/kdoc/89456264/
http://www.biyinjishi.com/kdoc/89456270/
http://www.biyinjishi.com/kdoc/89456257/
http://www.biyinjishi.com/kdoc/89456259/
http://www.biyinjishi.com/kdoc/89456260/
http://www.biyinjishi.com/kdoc/89456262/
http://www.biyinjishi.com/kdoc/89456265/
http://www.biyinjishi.com/kdoc/89456266/
http://www.biyinjishi.com/kdoc/89456269/
http://www.biyinjishi.com/kdoc/89456271/

C + + tips for throwing exceptions

Related Article

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.