Jump function setjmp and longjmp

setjmp and longjmp is a pair of jump function, the function is the same God, this function is more powerful than the goto, but functions almost like goto

Now, the code specifications in the request can not use goto, so setjmp and longjmp this is also the basic need.

 

Of course, the premise is a special case. Such as special treatment, to achieve their own exception handling code

 

On the interpretation of setjmp and longjmp not repeated, many online.

Here record the steps, how is it plainly:

 

 

With knowledge of the above simplistic later, look at the following code :( imitation try to write their own Niubi try it)

 

Then, as long as the longjmp code appeared and the second argument is not equal to 0, we can enter another process (catch)

Look, throw the code, when our own judgment to abnormal program, they can refer to the following:

 

 

 

Then, look at the catch code, it is actually very simple, is equivalent to processing

 

 If something other than this

 

 

This allows us to complete the jump. . . Is more clever application,

 

Guess you like

Origin www.cnblogs.com/winafa/p/11982405.html