C exception Handling: A SETJMP/LONGJMP mechanism for multilayer returns
exception Handling in C is implemented through the macro setjmp and macro longjmp defined in the Setjmp.h header file.
similar to the fallback stack, the SETJMP/LONGJMP mechanism is used in the following ways: annotate with setjmp macros where you want to do error handling the current "Environment" (function stack and register) is stored as an array in a possible error using the LONGJMP macro for control transfer restores the saved "environment" and returns the execution location to the next sentence of the SETJMP statement
Typical methods:
#include <iostream>
#include <csetjmp>
#include <cstdlib>
using NAMESPCE std;
Static jmp_buf env; An object of type jmp_buf is used to hold the environment
void f (int);
int main ()
{
/*setjmp () returns 0 when invoked for the first time, and if the control returned by LONGJMP () returns, the RET is reset to the second parameter of longjmp ().
int ret = setjmp (env);
if (1 = ret)
{
cout<< "Here is exception handling" <<endl;
Exit (0);
}
F (0);
return 0;
}
void f (int j)
{for
(; j<6;j++)
{
if (3==j)
longjmp (env,1);
}
}
Second, C + + exception handling: Try--catch--throw
Although C is also available in C + + , but there are a number of problems with this, such as skipping calls to class destructors and setjmp () overhead (saved information)
in fact, the literal meaning is almost understandable: put the statement you want to check in try{} add throw to the try{} module, which is the exception to throw catch{the module to catch an exception after the try{} module
Typical methods:
#include <iostream>
#include <cstdlib>
using NAMESPCE std;
Class myexception{};
void f (int) throw (myexception); Throw (myexception) is optional; (compared to Java), if throw (), it means that no exception
int main ()
{
try{
f (0)
is thrown; The catch (MyException &e) { //parameter list can have only types, excluding objects (compared to Java)
cout<< "Catching Exceptions";
}
return 0;
}
void f (Int j) throw (MyException)
{
throw myexception ();
}
Add:
(1) The try and catch blocks must be enclosed in curly braces, even if there is only one statement within the curly braces and the curly braces cannot be omitted;
(2) Try and catch must appear in pairs, a try_catch result can only have one try block, but can have more than one catch block to match the different exception information;
(3) If you do not specify the type of exception information in the catch block and use Shing "...", it means that it captures any type of exception information;
(4) If throw does not include any expression, it means that it throws the exception information that is currently being processed and passes it on to the catch on the previous level;
(5) in C + + once thrown an exception, if the program does not have any capture, then the system will automatically call a system function terminate, it calls the abort termination program;
iii. Java exception handling: try--catch--throw--finally
like C + + exception handling syntax, Java exception Handling in the "literal" look, only a more than C + + a Finally, but there are many differences in detail
usage: try{} module put the statement you want to check
put the throw statement exception to throw
catch{} module catch Exception
finally{} module execute this module regardless of whether or not to throw an exception, and in any case, finally perform the final module typical method:
Class MyException extends exception{}
Class test{
static void F () throws myexception{}//Note here throws MyException is required (than C + +), and Java throws, and C + + is throw
public static void Main (string[] args {
try{
f ();
}
catch (MyException e) {//catch must explicitly indicate the Parameter object
System.out.println ("Catch to Exception");
}
finally{
System.out.println ("Here is finally module");
}
}
}
Add:
Java also has the unchecked exception mechanism, the type of exception is runtimeexception or error subclasses, such an exception can not be thrown in the Try--catch code block, can be thrown directly, and it was caught by the system to terminate the program
Four, some additional C + + and Java comparison C + + can throw all types of objects, and Java can only throw Throwable subclass Java to throw an exception must be declared in the function header, and C + + is only recommended in this way Java must explicitly set an exception parameter object in the Catch module, and C + + is not required
comparison of the stack of C + +
assuming that the function f (int) is called recursively in the main function, F (3) causes an exception
This is C:
This is C + +: