純c實(shí)現(xiàn)異常捕獲try-catch組件教程示例
try / catch / finally / throw 介紹
本文用純c的代碼,實(shí)現(xiàn)異常捕獲try-catch組件。閱讀本文需要時(shí)刻牢記setjmp和longjmp的對(duì)應(yīng)關(guān)系。
在java,python,c++里面都有try catch異常捕獲。在try代碼塊里面執(zhí)行的函數(shù),如果出錯(cuò)有異常了,就會(huì)throw把異常拋出來(lái),拋出來(lái)的異常被catch接收進(jìn)行處理,而finally意味著無(wú)論有沒(méi)有異常,都會(huì)執(zhí)行finally代碼塊內(nèi)的代碼。
try{
connect_sql();//throw
}catch(){
}finally {
};
如何實(shí)現(xiàn)try-catch這一機(jī)制?
關(guān)于跳轉(zhuǎn),有兩個(gè)跳轉(zhuǎn)。那么在這里我們必然選用長(zhǎng)跳轉(zhuǎn)。
- goto:函數(shù)內(nèi)跳轉(zhuǎn),短跳轉(zhuǎn)
- setjmp/longjmp:跨函數(shù)跳轉(zhuǎn),長(zhǎng)跳轉(zhuǎn)
setjmp/longjmp這兩個(gè)函數(shù)是不存在壓棧出棧的,也就是說(shuō)longjmp跳轉(zhuǎn)到setjmp的地方后,會(huì)覆蓋之前的棧。
setjmp/longjmp使用介紹(重點(diǎn))
- setjmp(env):設(shè)置跳轉(zhuǎn)的位置,第一次返回0,后續(xù)返回longjmp的第二個(gè)參數(shù)
- longjmp(env, idx):跳轉(zhuǎn)到設(shè)置env的位置,第二個(gè)參數(shù)就是setjmp()的返回值
#include <stdio.h>
#include <setjmp.h>
jmp_buf env;
int count = 0;
void sub_func(int idx) {
printf("sub_func -->idx : %d\n", idx);
//第二個(gè)參數(shù)就是setjmp()的返回值
longjmp(env, idx);
}
int main() {
int idx = 0;
//設(shè)置跳轉(zhuǎn)標(biāo)簽,第一次返回0
count = setjmp(env);
if (count == 0) {
printf("count : %d\n", count);
sub_func(++idx);
}
else if (count == 1) {
printf("count : %d\n", count);
sub_func(++idx);
}
else if (count == 2) {
printf("count : %d\n", count);
sub_func(++idx);
}
else {
printf("other count \n");
}
return 0;
}
count : 0 sub_func -->idx : 1 count : 1 sub_func -->idx : 2 count : 2 sub_func -->idx : 3 other count
try-catch 和 setjmp/longjmp 的關(guān)系
try ---> setjmp(env) throw ---> longjmp(env,Exception) catch(Exception)

我們其實(shí)可以分析出來(lái),setjmp和count==0的地方,相當(dāng)于try,后面的else if 相當(dāng)于catch,最后一個(gè)else,其實(shí)并不是finally,因?yàn)閒inally是不管怎么樣都會(huì)執(zhí)行,上圖我標(biāo)注的其實(shí)是誤導(dǎo)的。應(yīng)該是下圖這樣才對(duì)。


宏定義實(shí)現(xiàn)try-catch Demo
4個(gè)關(guān)鍵字分析出來(lái)它們的關(guān)系之后,其實(shí)我們就能用宏定義來(lái)實(shí)現(xiàn)了。
#include <stdio.h>
#include <setjmp.h>
typedef struct _Exception {
jmp_buf env;
int exceptype;
} Exception;
#define Try(excep) if((excep.exceptype=setjmp(excep.env))==0)
#define Catch(excep, ExcepType) else if(excep.exceptype==ExcepType)
#define Throw(excep, ExcepType) longjmp(excep.env,ExcepType)
#define Finally
void throw_func(Exception ex, int idx) {
printf("throw_func -->idx : %d\n", idx);
Throw(ex, idx);
}
int main() {
int idx = 0;
Exception ex;
Try(ex) {
printf("ex.exceptype : %d\n", ex.exceptype);
throw_func(ex, ++idx);
}
Catch(ex, 1) {
printf("ex.exceptype : %d\n", ex.exceptype);
}
Catch(ex, 2) {
printf("ex.exceptype : %d\n", ex.exceptype);
}
Catch(ex, 3) {
printf("ex.exceptype : %d\n", ex.exceptype);
}
Finally{
printf("Finally\n");
};
return 0;
}
ex.exceptype : 0 throw_func -->idx : 1 ex.exceptype : 1 Finally

實(shí)現(xiàn)try-catch的三個(gè)問(wèn)題
雖然現(xiàn)在demo版看起來(lái)像這么回事了,但是還是有兩個(gè)問(wèn)題:
- 在哪個(gè)文件哪個(gè)函數(shù)哪個(gè)行拋的異常?
- try-catch嵌套怎么做?
- try-catch線程安全怎么做?
1. 在哪個(gè)文件哪個(gè)函數(shù)哪個(gè)行拋的異常
系統(tǒng)提供了三個(gè)宏可以供我們使用,如果我們沒(méi)有catch到異常,我們就可以打印出來(lái)
__func__, __FILE__, __LINE__
2. try-catch嵌套怎么做?
我們知道try-catch是可以嵌套的,那么這就形成了一個(gè)棧的數(shù)據(jù)結(jié)構(gòu),現(xiàn)在下面有三個(gè)try,每個(gè)setjmp對(duì)應(yīng)的都是不同的jmp_buf,那么我們可以定義一個(gè)jmp_buf的棧。
try{
try{
try{
}catch(){
}
}catch(){
}
}catch(){
}finally{
};
那么我們很容易能寫(xiě)出來(lái),既然是棧,try的時(shí)候我們就插入一個(gè)結(jié)點(diǎn),catch的時(shí)候我們就pop一個(gè)出來(lái)。
#define EXCEPTION_MESSAGE_LENGTH 512
typedef struct _ntyException {
const char *name;
} ntyException;
ntyException SQLException = {"SQLException"};
ntyException TimeoutException = {"TimeoutException"};
typedef struct _ntyExceptionFrame {
jmp_buf env;
int line;
const char *func;
const char *file;
ntyException *exception;
struct _ntyExceptionFrame *next;
char message[EXCEPTION_MESSAGE_LENGTH + 1];
} ntyExceptionFrame;
enum {
ExceptionEntered = 0,//0
ExceptionThrown, //1
ExceptionHandled, //2
ExceptionFinalized//3
};
3. try-catch線程安全
每個(gè)線程都可以try-catch,但是我們以及知道了是個(gè)棧結(jié)構(gòu),既ExceptionStack,那么每個(gè)線程是獨(dú)有一個(gè)ExceptionStack呢?還是共享同一個(gè)ExceptionStack?很明顯,A線程的異常應(yīng)該有A的處理,而不是由B線程處理。那么我們就使用Linux線程私有數(shù)據(jù)Thread-specific Data(TSD)來(lái)做。
/* ** **** ******** **************** Thread safety **************** ******** **** ** */
#define ntyThreadLocalData pthread_key_t
#define ntyThreadLocalDataSet(key, value) pthread_setspecific((key), (value))
#define ntyThreadLocalDataGet(key) pthread_getspecific((key))
#define ntyThreadLocalDataCreate(key) pthread_key_create(&(key), NULL)
ntyThreadLocalData ExceptionStack;
static void init_once(void) {
ntyThreadLocalDataCreate(ExceptionStack);
}
static pthread_once_t once_control = PTHREAD_ONCE_INIT;
void ntyExceptionInit(void) {
pthread_once(&once_control, init_once);
}
代碼實(shí)現(xiàn)與解釋
try
首先創(chuàng)建一個(gè)新節(jié)點(diǎn)入棧,然后setjmp設(shè)置一個(gè)標(biāo)記,接下來(lái)就是大括號(hào)里面的操作了,如果有異常,那么就會(huì)被throw拋出來(lái),為什么這里最后一行是if?因?yàn)閘ongjmp的時(shí)候,返回的地方是setjmp,不要忘了!要時(shí)刻扣住longjmp和setjmp。
#define Try do { \
volatile int Exception_flag; \
ntyExceptionFrame frame; \
frame.message[0] = 0; \
frame.next = (ntyExceptionFrame*)ntyThreadLocalDataGet(ExceptionStack); \
ntyThreadLocalDataSet(ExceptionStack, &frame); \
Exception_flag = setjmp(frame.env); \
if (Exception_flag == ExceptionEntered) {
Try{
//...
Throw(A, "A");
}
throw
在這里,我們不應(yīng)該把throw定義成宏,而應(yīng)該定義成函數(shù)。這里分兩類,一類是try里面的throw,一類是沒(méi)有try直接throw。
- 對(duì)于try里面的異常,我們將其狀態(tài)變成ExceptionThrown,然后longjmp到setjmp的地方,由catch處理
- 對(duì)于直接拋的異常,必然沒(méi)有catch去捕獲,那么我們直接打印出來(lái)
- 如果第一種情況的異常,沒(méi)有被catch捕獲到怎么辦呢?后面會(huì)被ReThrow出來(lái),對(duì)于再次被拋出,我們就直接進(jìn)行打印異常
這里的##__VA_ARGS__是可變參數(shù),具體不多介紹了,不是本文重點(diǎn)。
#define ReThrow ntyExceptionThrow(frame.exception, frame.func, frame.file, frame.line, NULL) #define Throw(e, cause, ...) ntyExceptionThrow(&(e), __func__, __FILE__, __LINE__, cause, ##__VA_ARGS__,NULL)
void ntyExceptionThrow(ntyException *excep, const char *func, const char *file, int line, const char *cause, ...) {
va_list ap;
ntyExceptionFrame *frame = (ntyExceptionFrame *) ntyThreadLocalDataGet(ExceptionStack);
if (frame) {
//異常名
frame->exception = excep;
frame->func = func;
frame->file = file;
frame->line = line;
//異常打印的信息
if (cause) {
va_start(ap, cause);
vsnprintf(frame->message, EXCEPTION_MESSAGE_LENGTH, cause, ap);
va_end(ap);
}
ntyExceptionPopStack;
longjmp(frame->env, ExceptionThrown);
}
//沒(méi)有被catch,直接throw
else if (cause) {
char message[EXCEPTION_MESSAGE_LENGTH + 1];
va_start(ap, cause);
vsnprintf(message, EXCEPTION_MESSAGE_LENGTH, cause, ap);
va_end(ap);
printf("%s: %s\n raised in %s at %s:%d\n", excep->name, message, func ? func : "?", file ? file : "?", line);
}
else {
printf("%s: %p\n raised in %s at %s:%d\n", excep->name, excep, func ? func : "?", file ? file : "?", line);
}
}
Catch
如果還是ExceptionEntered狀態(tài),說(shuō)明沒(méi)有異常,沒(méi)有throw。如果捕獲到異常了,那么其狀態(tài)就是ExceptionHandled。
#define Catch(nty_exception) \
if (Exception_flag == ExceptionEntered) ntyExceptionPopStack; \
} else if (frame.exception == &(nty_exception)) { \
Exception_flag = ExceptionHandled;
Finally
finally也是一樣,如果還是ExceptionEntered狀態(tài),說(shuō)明沒(méi)有異常沒(méi)有捕獲,那么現(xiàn)在狀態(tài)是終止階段。
#define Finally \
if (Exception_flag == ExceptionEntered) ntyExceptionPopStack; \
} { \
if (Exception_flag == ExceptionEntered) \
Exception_flag = ExceptionFinalized;
EndTry
有些人看到EndTry可能會(huì)有疑問(wèn),try-catch一共不就4個(gè)關(guān)鍵字嗎?怎么你多了一個(gè)。我們先來(lái)看看EndTry做了什么,首先如果是ExceptionEntered狀態(tài),那意味著什么?意味著沒(méi)有throw,沒(méi)有catch,沒(méi)有finally,只有try,我們需要對(duì)這種情況進(jìn)行處理,要出棧。
還有一種情況,如果是ExceptionThrown狀態(tài),說(shuō)明什么?沒(méi)有被catch捕獲到,那么我們就再次拋出,進(jìn)行打印錯(cuò)誤。至于為什么多個(gè)EndTry,寫(xiě)起來(lái)方便唄~
#define EndTry \
if (Exception_flag == ExceptionEntered) ntyExceptionPopStack; \
} if (Exception_flag == ExceptionThrown) {ReThrow;} \
} while (0)
try-catch代碼
/* ** **** ******** **************** try / catch / finally / throw **************** ******** **** ** */
#define EXCEPTION_MESSAGE_LENGTH 512
typedef struct _ntyException {
const char *name;
} ntyException;
ntyException SQLException = {"SQLException"};
ntyException TimeoutException = {"TimeoutException"};
typedef struct _ntyExceptionFrame {
jmp_buf env;
int line;
const char *func;
const char *file;
ntyException *exception;
struct _ntyExceptionFrame *next;
char message[EXCEPTION_MESSAGE_LENGTH + 1];
} ntyExceptionFrame;
enum {
ExceptionEntered = 0,//0
ExceptionThrown, //1
ExceptionHandled, //2
ExceptionFinalized//3
};
#define ntyExceptionPopStack \
ntyThreadLocalDataSet(ExceptionStack, ((ntyExceptionFrame*)ntyThreadLocalDataGet(ExceptionStack))->next)
#define ReThrow ntyExceptionThrow(frame.exception, frame.func, frame.file, frame.line, NULL)
#define Throw(e, cause, ...) ntyExceptionThrow(&(e), __func__, __FILE__, __LINE__, cause, ##__VA_ARGS__,NULL)
#define Try do { \
volatile int Exception_flag; \
ntyExceptionFrame frame; \
frame.message[0] = 0; \
frame.next = (ntyExceptionFrame*)ntyThreadLocalDataGet(ExceptionStack); \
ntyThreadLocalDataSet(ExceptionStack, &frame); \
Exception_flag = setjmp(frame.env); \
if (Exception_flag == ExceptionEntered) {
#define Catch(nty_exception) \
if (Exception_flag == ExceptionEntered) ntyExceptionPopStack; \
} else if (frame.exception == &(nty_exception)) { \
Exception_flag = ExceptionHandled;
#define Finally \
if (Exception_flag == ExceptionEntered) ntyExceptionPopStack; \
} { \
if (Exception_flag == ExceptionEntered) \
Exception_flag = ExceptionFinalized;
#define EndTry \
if (Exception_flag == ExceptionEntered) ntyExceptionPopStack; \
} if (Exception_flag == ExceptionThrown) {ReThrow;} \
} while (0)
void ntyExceptionThrow(ntyException *excep, const char *func, const char *file, int line, const char *cause, ...) {
va_list ap;
ntyExceptionFrame *frame = (ntyExceptionFrame *) ntyThreadLocalDataGet(ExceptionStack);
if (frame) {
//異常名
frame->exception = excep;
frame->func = func;
frame->file = file;
frame->line = line;
//異常打印的信息
if (cause) {
va_start(ap, cause);
vsnprintf(frame->message, EXCEPTION_MESSAGE_LENGTH, cause, ap);
va_end(ap);
}
ntyExceptionPopStack;
longjmp(frame->env, ExceptionThrown);
}
//沒(méi)有被catch,直接throw
else if (cause) {
char message[EXCEPTION_MESSAGE_LENGTH + 1];
va_start(ap, cause);
vsnprintf(message, EXCEPTION_MESSAGE_LENGTH, cause, ap);
va_end(ap);
printf("%s: %s\n raised in %s at %s:%d\n", excep->name, message, func ? func : "?", file ? file : "?", line);
}
else {
printf("%s: %p\n raised in %s at %s:%d\n", excep->name, excep, func ? func : "?", file ? file : "?", line);
}
}
Debug測(cè)試代碼
/* ** **** ******** **************** debug **************** ******** **** ** */
ntyException A = {"AException"};
ntyException B = {"BException"};
ntyException C = {"CException"};
ntyException D = {"DException"};
void *thread(void *args) {
pthread_t selfid = pthread_self();
Try
{
Throw(A, "A");
}
Catch (A)
{
printf("catch A : %ld\n", selfid);
}
EndTry;
Try
{
Throw(B, "B");
}
Catch (B)
{
printf("catch B : %ld\n", selfid);
}
EndTry;
Try
{
Throw(C, "C");
}
Catch (C)
{
printf("catch C : %ld\n", selfid);
}
EndTry;
Try
{
Throw(D, "D");
}
Catch (D)
{
printf("catch D : %ld\n", selfid);
}
EndTry;
Try
{
Throw(A, "A Again");
Throw(B, "B Again");
Throw(C, "C Again");
Throw(D, "D Again");
}
Catch (A)
{
printf("catch A again : %ld\n", selfid);
}
Catch (B)
{
printf("catch B again : %ld\n", selfid);
}
Catch (C)
{
printf("catch C again : %ld\n", selfid);
}
Catch (D)
{
printf("catch B again : %ld\n", selfid);
}
EndTry;
}
#define PTHREAD_NUM 8
int main(void) {
ntyExceptionInit();
printf("\n\n=> Test1: Throw\n");
{
Throw(D, NULL); //ntyExceptionThrow(&(D), "_function_name_", "_file_name_", 202, ((void *) 0), ((void *) 0))
Throw(C, "null C"); //ntyExceptionThrow(&(C), "_function_name_", "_file_name_", 203, "null C", ((void *) 0))
}
printf("=> Test1: Ok\n\n");
printf("\n\n=> Test2: Try-Catch Double Nesting\n");
{
Try
{
Try
{
Throw(B, "call B");
}
Catch (B)
{
printf("catch B \n");
}
EndTry;
Throw(A, NULL);
}
Catch(A)
{
printf("catch A \n");
printf("Result: Ok\n");
}
EndTry;
}
printf("=> Test2: Ok\n\n");
printf("\n\n=> Test3: Try-Catch Triple Nesting\n");
{
Try
{
Try
{
Try
{
Throw(C, "call C");
}
Catch (C)
{
printf("catch C\n");
}
EndTry;
Throw(B, "call B");
}
Catch (B)
{
printf("catch B\n");
}
EndTry;
Throw(A, NULL);
}
Catch(A)
{
printf("catch A\n");
}
EndTry;
}
printf("=> Test3: Ok\n\n");
printf("=> Test4: Test Thread-safeness\n");
int i = 0;
pthread_t th_id[PTHREAD_NUM];
for (i = 0; i < PTHREAD_NUM; i++) {
pthread_create(&th_id[i], NULL, thread, NULL);
}
for (i = 0; i < PTHREAD_NUM; i++) {
pthread_join(th_id[i], NULL);
}
printf("=> Test4: Ok\n\n");
printf("\n\n=> Test5: No Success Catch\n");
{
Try
{
Throw(A, "no catch A ,should Rethrow");
}
EndTry;
}
printf("=> Test5: Rethrow Success\n\n");
printf("\n\n=> Test6: Normal Test\n");
{
Try
{
Throw(A, "call A");
}
Catch(A)
{
printf("catch A\n");
}
Finally
{
printf("wxf nb\n");
};
EndTry;
}
printf("=> Test6: ok\n\n");
}
線程安全、try-catch、Debug測(cè)試代碼 匯總
#include <stdio.h>
#include <setjmp.h>
#include <pthread.h>
#include <stdarg.h>
/* ** **** ******** **************** Thread safety **************** ******** **** ** */
#define ntyThreadLocalData pthread_key_t
#define ntyThreadLocalDataSet(key, value) pthread_setspecific((key), (value))
#define ntyThreadLocalDataGet(key) pthread_getspecific((key))
#define ntyThreadLocalDataCreate(key) pthread_key_create(&(key), NULL)
ntyThreadLocalData ExceptionStack;
static void init_once(void) {
ntyThreadLocalDataCreate(ExceptionStack);
}
static pthread_once_t once_control = PTHREAD_ONCE_INIT;
void ntyExceptionInit(void) {
pthread_once(&once_control, init_once);
}
/* ** **** ******** **************** try / catch / finally / throw **************** ******** **** ** */
#define EXCEPTION_MESSAGE_LENGTH 512
typedef struct _ntyException {
const char *name;
} ntyException;
ntyException SQLException = {"SQLException"};
ntyException TimeoutException = {"TimeoutException"};
typedef struct _ntyExceptionFrame {
jmp_buf env;
int line;
const char *func;
const char *file;
ntyException *exception;
struct _ntyExceptionFrame *next;
char message[EXCEPTION_MESSAGE_LENGTH + 1];
} ntyExceptionFrame;
enum {
ExceptionEntered = 0,//0
ExceptionThrown, //1
ExceptionHandled, //2
ExceptionFinalized//3
};
#define ntyExceptionPopStack \
ntyThreadLocalDataSet(ExceptionStack, ((ntyExceptionFrame*)ntyThreadLocalDataGet(ExceptionStack))->next)
#define ReThrow ntyExceptionThrow(frame.exception, frame.func, frame.file, frame.line, NULL)
#define Throw(e, cause, ...) ntyExceptionThrow(&(e), __func__, __FILE__, __LINE__, cause, ##__VA_ARGS__,NULL)
#define Try do { \
volatile int Exception_flag; \
ntyExceptionFrame frame; \
frame.message[0] = 0; \
frame.next = (ntyExceptionFrame*)ntyThreadLocalDataGet(ExceptionStack); \
ntyThreadLocalDataSet(ExceptionStack, &frame); \
Exception_flag = setjmp(frame.env); \
if (Exception_flag == ExceptionEntered) {
#define Catch(nty_exception) \
if (Exception_flag == ExceptionEntered) ntyExceptionPopStack; \
} else if (frame.exception == &(nty_exception)) { \
Exception_flag = ExceptionHandled;
#define Finally \
if (Exception_flag == ExceptionEntered) ntyExceptionPopStack; \
} { \
if (Exception_flag == ExceptionEntered) \
Exception_flag = ExceptionFinalized;
#define EndTry \
if (Exception_flag == ExceptionEntered) ntyExceptionPopStack; \
} if (Exception_flag == ExceptionThrown) {ReThrow;} \
} while (0)
void ntyExceptionThrow(ntyException *excep, const char *func, const char *file, int line, const char *cause, ...) {
va_list ap;
ntyExceptionFrame *frame = (ntyExceptionFrame *) ntyThreadLocalDataGet(ExceptionStack);
if (frame) {
//異常名
frame->exception = excep;
frame->func = func;
frame->file = file;
frame->line = line;
//異常打印的信息
if (cause) {
va_start(ap, cause);
vsnprintf(frame->message, EXCEPTION_MESSAGE_LENGTH, cause, ap);
va_end(ap);
}
ntyExceptionPopStack;
longjmp(frame->env, ExceptionThrown);
}
//沒(méi)有被catch,直接throw
else if (cause) {
char message[EXCEPTION_MESSAGE_LENGTH + 1];
va_start(ap, cause);
vsnprintf(message, EXCEPTION_MESSAGE_LENGTH, cause, ap);
va_end(ap);
printf("%s: %s\n raised in %s at %s:%d\n", excep->name, message, func ? func : "?", file ? file : "?", line);
}
else {
printf("%s: %p\n raised in %s at %s:%d\n", excep->name, excep, func ? func : "?", file ? file : "?", line);
}
}
/* ** **** ******** **************** debug **************** ******** **** ** */
ntyException A = {"AException"};
ntyException B = {"BException"};
ntyException C = {"CException"};
ntyException D = {"DException"};
void *thread(void *args) {
pthread_t selfid = pthread_self();
Try
{
Throw(A, "A");
}
Catch (A)
{
printf("catch A : %ld\n", selfid);
}
EndTry;
Try
{
Throw(B, "B");
}
Catch (B)
{
printf("catch B : %ld\n", selfid);
}
EndTry;
Try
{
Throw(C, "C");
}
Catch (C)
{
printf("catch C : %ld\n", selfid);
}
EndTry;
Try
{
Throw(D, "D");
}
Catch (D)
{
printf("catch D : %ld\n", selfid);
}
EndTry;
Try
{
Throw(A, "A Again");
Throw(B, "B Again");
Throw(C, "C Again");
Throw(D, "D Again");
}
Catch (A)
{
printf("catch A again : %ld\n", selfid);
}
Catch (B)
{
printf("catch B again : %ld\n", selfid);
}
Catch (C)
{
printf("catch C again : %ld\n", selfid);
}
Catch (D)
{
printf("catch B again : %ld\n", selfid);
}
EndTry;
}
#define PTHREAD_NUM 8
int main(void) {
ntyExceptionInit();
printf("\n\n=> Test1: Throw\n");
{
Throw(D, NULL); //ntyExceptionThrow(&(D), "_function_name_", "_file_name_", 202, ((void *) 0), ((void *) 0))
Throw(C, "null C"); //ntyExceptionThrow(&(C), "_function_name_", "_file_name_", 203, "null C", ((void *) 0))
}
printf("=> Test1: Ok\n\n");
printf("\n\n=> Test2: Try-Catch Double Nesting\n");
{
Try
{
Try
{
Throw(B, "call B");
}
Catch (B)
{
printf("catch B \n");
}
EndTry;
Throw(A, NULL);
}
Catch(A)
{
printf("catch A \n");
printf("Result: Ok\n");
}
EndTry;
}
printf("=> Test2: Ok\n\n");
printf("\n\n=> Test3: Try-Catch Triple Nesting\n");
{
Try
{
Try
{
Try
{
Throw(C, "call C");
}
Catch (C)
{
printf("catch C\n");
}
EndTry;
Throw(B, "call B");
}
Catch (B)
{
printf("catch B\n");
}
EndTry;
Throw(A, NULL);
}
Catch(A)
{
printf("catch A\n");
}
EndTry;
}
printf("=> Test3: Ok\n\n");
printf("=> Test4: Test Thread-safeness\n");
int i = 0;
pthread_t th_id[PTHREAD_NUM];
for (i = 0; i < PTHREAD_NUM; i++) {
pthread_create(&th_id[i], NULL, thread, NULL);
}
for (i = 0; i < PTHREAD_NUM; i++) {
pthread_join(th_id[i], NULL);
}
printf("=> Test4: Ok\n\n");
printf("\n\n=> Test5: No Success Catch\n");
{
Try
{
Throw(A, "no catch A ,should Rethrow");
}
EndTry;
}
printf("=> Test5: Rethrow Success\n\n");
printf("\n\n=> Test6: Normal Test\n");
{
Try
{
Throw(A, "call A");
}
Catch(A)
{
printf("catch A\n");
}
Finally
{
printf("wxf nb\n");
};
EndTry;
}
printf("=> Test6: ok\n\n");
}
void all() {
///////////try
do {
volatile int Exception_flag;
ntyExceptionFrame frame;
frame.message[0] = 0;
frame.next = (ntyExceptionFrame *) pthread_getspecific((ExceptionStack));
pthread_setspecific((ExceptionStack), (&frame));
Exception_flag = _setjmp(frame.env);
if (Exception_flag == ExceptionEntered) {
///////////
{
///////////try
do {
volatile int Exception_flag;
ntyExceptionFrame frame;
frame.message[0] = 0;
frame.next = (ntyExceptionFrame *) pthread_getspecific((ExceptionStack));
pthread_setspecific((ExceptionStack), (&frame));
Exception_flag = _setjmp(frame.env);
if (Exception_flag == ExceptionEntered) {
///////////
{
///////////Throw(B, "recall B"); --->longjmp ExceptionThrown
ntyExceptionThrow(&(B), "_function_name_", "_file_name_", 302, "recall B", ((void *) 0));
///////////
}
///////////Catch (B)
if (Exception_flag == ExceptionEntered)
ntyExceptionPopStack;
}
else if (frame.exception == &(B)) {
Exception_flag = ExceptionHandled;
///////////
{ ///////////
printf("recall B \n");
///////////
}
////////fin
if (Exception_flag == ExceptionEntered)
ntyExceptionPopStack;
if (Exception_flag == ExceptionEntered)
Exception_flag = ExceptionFinalized;
/////////////////{
{
printf("fin\n");
};
////////////////////}
///////////EndTry;
if (Exception_flag == ExceptionEntered)
ntyExceptionPopStack;
}
if (Exception_flag == ExceptionThrown)
ntyExceptionThrow(frame.exception, frame.func, frame.file, frame.line, ((void *) 0));
} while (0);
///////////Throw(A, NULL); longjmp ExceptionThrown
ntyExceptionThrow(&(A), "_function_name_", "_file_name_", 329, ((void *) 0), ((void *) 0));
///////////
}
///////////Catch(A)
if (Exception_flag == ExceptionEntered)
ntyExceptionPopStack;
}
else if (frame.exception == &(A)) {
Exception_flag = ExceptionHandled;
///////////
{
///////////
printf("\tResult: Ok\n");
///////////
}
/////////// EndTry;
if (Exception_flag == ExceptionEntered)
ntyExceptionPopStack;
}
if (Exception_flag == ExceptionThrown)
ntyExceptionThrow(frame.exception, frame.func, frame.file, frame.line, ((void *) 0));
} while (0);
///////////
}以上就是純c實(shí)現(xiàn)異常捕獲try-catch組件教程示例的詳細(xì)內(nèi)容,更多關(guān)于c異常捕獲try-catch的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C++實(shí)現(xiàn)數(shù)組中元素組合出最大值
這篇文章主要介紹了C++實(shí)現(xiàn)數(shù)組中元素組合出最大值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05
C語(yǔ)言 數(shù)據(jù)結(jié)構(gòu)之中序二叉樹(shù)實(shí)例詳解
這篇文章主要介紹了C語(yǔ)言 數(shù)據(jù)結(jié)構(gòu)之中序二叉樹(shù)實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-01-01
MySQL系列教程之使用C語(yǔ)言來(lái)連接數(shù)據(jù)庫(kù)
c語(yǔ)言操作Mysql數(shù)據(jù)庫(kù),主要就是為了實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)的增、刪、改、查等操作,下面這篇文章主要給大家介紹了關(guān)于MySQL系列教程之使用C語(yǔ)言來(lái)連接數(shù)據(jù)庫(kù)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09
C++筆記-設(shè)置cout輸出數(shù)據(jù)的寬度和填充方式
這篇文章主要介紹了C++筆記-設(shè)置cout輸出數(shù)據(jù)的寬度和填充方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
C語(yǔ)言之malloc動(dòng)態(tài)分配內(nèi)存和free釋放
這篇文章主要介紹了C語(yǔ)言之malloc動(dòng)態(tài)分配內(nèi)存和free釋放,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
C++二叉搜索樹(shù)模擬實(shí)現(xiàn)示例
本文主要介紹了C++二叉搜索樹(shù)模擬實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11

