C + + Exception exception mechanism

Source: Internet
Author: User

C + + Exceptions are a response to an exception that occurs during the loss of a program (for example, by 0). Exceptions provide a way to pass control from one part of the program to another. There are 3 components to handling Exceptions:
* Throws an exception;
* Catch exceptions using Handlers;
* Use try block.
The program throws an exception when a problem occurs. The throw statement is actually a jump, that is, the command program jumps to another statement. The Throw keyword indicates that an exception was thrown, followed by a value (such as a string or object) that indicates the characteristics of the exception.
The program uses an exception handler (exception handler) to catch the exception, which is in the program to handle the problem. The Catch keyword indicates a catch exception. The handler begins with a keyword catch, followed by a type declaration in parentheses, which indicates the type of exception the exception handler is responding to, and then a block of code enclosed in curly braces that indicates the action to take. The Catch keyword and the exception type are used as labels, indicating that when an exception is thrown, the program should jump to that location to execute. An exception handler is also known as a catch block.
A try block identifies a block of code where a particular exception may be activated, followed by one or more catch blocks. The try block is indicated by the keyword try, which is followed by a block of code enclosed in curly braces, indicating that you need to be aware of the exceptions thrown by the code.
The simplest way to understand how these 3 elements work together is to look at a short example, as follows.
Error3.cpp

//Error3.cpp--Using an exception#include <iostream>DoubleHmean (DoubleADoubleb);intMain () {Doublex, y, Z; Std::cout<<"Enter The numbers:";  while(Std::cin >> x >>y) {Try{z= Hmean (x, y);//start of Try block}//End of Try block        Catch(Const Char* s)//start of exception handler{std::cout<< s <<Std::endl; Std::cout<<"Enter A new pair of numbers:"; Continue; } std::cout<<"Harmonic mean of"<< x <<" and"<<y<<" is"<< Z <<Std::endl; Std::cout<<"Enter Next set of numbers <q to Quit>:"; } std::cout<<"bye!\n"; return 0;}DoubleHmean (DoubleADoubleb) {    if(A = =-b)Throw "Bad hmean () arguments:a =-B not allowed"; return 2.0* A * b/(A +b);}

Effect:

Enter numbers:3 6Harmonic mean of 3 and 6 is 4Enter next set of the numbers <q to Quit>: 10-10bad Hmean () argument S:a = B Not allowedenter a new pair of numbers:1 19Harmonic mean of 1 and are 1.9Enter next set of numbers <q to Q Uit>: qbye!

Program flow When an exception occurs:
1. The program calls Hmean () in the try block.
2.hmean () throws an exception, which executes the catch block and pays the exception string to S.
The 3.catch block returns to the start of the while loop as a value.

C + + Exception exception mechanism

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.