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 (...).