Try catch throw

Source: Internet
Author: User
Tags try catch

Class cmyexception
{};
Try
{
Int ntemp = 0;
If (ntemp = 0)
{
Throw (ntemp); // It can also be written as throw ntemp;
// Directly write it as throw. Throw is an exception generated before this operation. Do not write it like this. errors may occur during debugging and running!
// It is best to write this method only in the catch () Statement block! Throw; Re-Throw the original exception instead of the original exception Copy 1
}
Cmyexception myexception;
Throw (myexception );
}
Catch (INT nerror) // capture the int type
{
}
Catch (cmyexception & error) // catch the cmyexception type, which must be referenced
{
}
Catch (...) // catch other types, but the previous ones will no longer be seen.
{

Throw; // throw an exception that cannot be handled without any parameters
}
Some operations cannot be correctly executed under certain conditions. Only some information can be thrown to indicate that the upper-layer operation fails, if exceptions are not needed, they can also be expressed by error codes (specifically, exceptions are normal operation of the program)
To apply exceptions, you must put the actual running code in the try block and throw various types of exceptions, such as int type, cmyexception type (of course, you will not throw some exceptions by yourself. Generally, some functions that may throw some exceptions, such as some API functions, are called in the try block ), if an exception occurs during the execution of the program, the catch block will be used, but note that there can be many catch blocks at the same time (as shown above) however, only exceptions of the same type can be caught in catch. For example, catch (INT nerror) can only catch int type exceptions, and catch (cmyexception & error) can only catch exceptions of the cmyexception type. Finally, catch (...) can capture all types of exceptions that cannot match the previous one.
-------- Throw ----------- after the function name or Class Name -------------
Throw () The function does not throw an exception. // If throw exists in the program, a warning is reported during compilation.
Throw (...) the function can throw an exception.
Throw (type) the function can throw an exception of type. However, in Visual C ++. net, this is equivalent to throw (...).

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.