C + + Exception capture

Source: Internet
Author: User
Tags integer numbers

#include <iostream>
#include <string>
using namespace Std;

int main ()
{
Try
{
int value1,value2; Define two integer variables
cout<< "Pleaseinput-value:" <<endl; Prompt to enter information
cin>>value1>>value2; Enter two integer numbers from the keyboard
cout<< "Maybeexception Code:" <<endl; Code message indicating that an exception may occur
if (value2== 0)//If the divisor is 0 throws an exception
{
Throw 0;
}
else//Otherwise direct COMPUTE division operation
{
cout<< "Value1/value2:" << (value1/value2) <<endl;
}
}catch (int i)//catch parameter is an integer type exception
{
cout<< "Divisoris 0!" <<endl; Exception handling code
}
return 0;
}

Once an exception is thrown, a catch is executed, no corresponding catch program is thrown, and a statement that is not executed behind the try is no longer executed.

A try body can throw an exception directly or indirectly in the body of a function called in the try body.

A try block can contain one or more exception throw points. It is important to note, however, that if an exception is thrown, the corresponding catch block is snapped, and the following code body execution in the try block is terminated. The code executes directly into the corresponding catch block, and the last catch block executes after the exception has been processed and jumps directly to the catch block corresponding to all the current try blocks.

try{

Throw ....

}catch () {}

C + + Exception capture

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.