亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么使用純C語言實現異常捕獲try-catch組件

發布時間:2022-08-08 10:55:48 來源:億速云 閱讀:342 作者:iii 欄目:開發技術

這篇文章主要介紹“怎么使用純C語言實現異常捕獲try-catch組件”的相關知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“怎么使用純C語言實現異常捕獲try-catch組件”文章能幫助大家解決問題。

try / catch / finally / throw 介紹

在java,python,c++里面都有try catch異常捕獲。在try代碼塊里面執行的函數,如果出錯有異常了,就會throw把異常拋出來,拋出來的異常被catch接收進行處理,而finally意味著無論有沒有異常,都會執行finally代碼塊內的代碼。

try{
    connect_sql();//throw
}catch(){
}finally {
};

如何實現try-catch這一機制?

關于跳轉,有兩個跳轉。那么在這里我們必然選用長跳轉。

  • goto:函數內跳轉,短跳轉

  • setjmp/longjmp:跨函數跳轉,長跳轉

setjmp/longjmp這兩個函數是不存在壓棧出棧的,也就是說longjmp跳轉到setjmp的地方后,會覆蓋之前的棧。

setjmp/longjmp使用介紹(重點)

  • setjmp(env):設置跳轉的位置,第一次返回0,后續返回longjmp的第二個參數

  • longjmp(env, idx):跳轉到設置env的位置,第二個參數就是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);
    //第二個參數就是setjmp()的返回值
    longjmp(env, idx);
}
int main() {
    int idx = 0;
    //設置跳轉標簽,第一次返回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 的關系

try ---&gt; setjmp(env)
throw ---&gt; longjmp(env,Exception)
catch(Exception)

怎么使用純C語言實現異常捕獲try-catch組件

我們其實可以分析出來,setjmp和count==0的地方,相當于try,后面的else if 相當于catch,最后一個else,其實并不是finally,因為finally是不管怎么樣都會執行,上圖我標注的其實是誤導的。應該是下圖這樣才對。

怎么使用純C語言實現異常捕獲try-catch組件

怎么使用純C語言實現異常捕獲try-catch組件

宏定義實現try-catch Demo

4個關鍵字分析出來它們的關系之后,其實我們就能用宏定義來實現了。

#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

怎么使用純C語言實現異常捕獲try-catch組件

實現try-catch的三個問題

雖然現在demo版看起來像這么回事了,但是還是有兩個問題:

  • 在哪個文件哪個函數哪個行拋的異常?

  • try-catch嵌套怎么做?

  • try-catch線程安全怎么做?

1. 在哪個文件哪個函數哪個行拋的異常

系統提供了三個宏可以供我們使用,如果我們沒有catch到異常,我們就可以打印出來

__func__, __FILE__, __LINE__

2. try-catch嵌套怎么做?

我們知道try-catch是可以嵌套的,那么這就形成了一個棧的數據結構,現在下面有三個try,每個setjmp對應的都是不同的jmp_buf,那么我們可以定義一個jmp_buf的棧。

try{
    try{
        try{
        }catch(){
        }
    }catch(){
    }
}catch(){
}finally{
};

那么我們很容易能寫出來,既然是棧,try的時候我們就插入一個結點,catch的時候我們就pop一個出來。

#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線程安全

每個線程都可以try-catch,但是我們以及知道了是個棧結構,既ExceptionStack,那么每個線程是獨有一個ExceptionStack呢?還是共享同一個ExceptionStack?很明顯,A線程的異常應該有A的處理,而不是由B線程處理。那么我們就使用Linux線程私有數據Thread-specific Data(TSD)來做。

/* ** **** ******** **************** 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

首先創建一個新節點入棧,然后setjmp設置一個標記,接下來就是大括號里面的操作了,如果有異常,那么就會被throw拋出來,為什么這里最后一行是if?因為longjmp的時候,返回的地方是setjmp,不要忘了!要時刻扣住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

在這里,我們不應該把throw定義成宏,而應該定義成函數。這里分兩類,一類是try里面的throw,一類是沒有try直接throw。

  • 對于try里面的異常,我們將其狀態變成ExceptionThrown,然后longjmp到setjmp的地方,由catch處理

  • 對于直接拋的異常,必然沒有catch去捕獲,那么我們直接打印出來

  • 如果第一種情況的異常,沒有被catch捕獲到怎么辦呢?后面會被ReThrow出來,對于再次被拋出,我們就直接進行打印異常

這里的##__VA_ARGS__是可變參數,具體不多介紹了,不是本文重點。

#define ReThrow                    ntyExceptionThrow(frame.exception, frame.func, frame.file, frame.line, NULL)
#define Throw(e, cause, ...)    ntyExceptionThrow(&amp;(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);
    }
        //沒有被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狀態,說明沒有異常,沒有throw。如果捕獲到異常了,那么其狀態就是ExceptionHandled。

#define Catch(nty_exception) \
                if (Exception_flag == ExceptionEntered) ntyExceptionPopStack; \
            } else if (frame.exception == &(nty_exception)) { \
                Exception_flag = ExceptionHandled;

Finally

finally也是一樣,如果還是ExceptionEntered狀態,說明沒有異常沒有捕獲,那么現在狀態是終止階段。

#define Finally \
                if (Exception_flag == ExceptionEntered) ntyExceptionPopStack; \
            } { \
                if (Exception_flag == ExceptionEntered)    \
                    Exception_flag = ExceptionFinalized;

EndTry

有些人看到EndTry可能會有疑問,try-catch一共不就4個關鍵字嗎?怎么你多了一個。我們先來看看EndTry做了什么,首先如果是ExceptionEntered狀態,那意味著什么?意味著沒有throw,沒有catch,沒有finally,只有try,我們需要對這種情況進行處理,要出棧。

還有一種情況,如果是ExceptionThrown狀態,說明什么?沒有被catch捕獲到,那么我們就再次拋出,進行打印錯誤。至于為什么多個EndTry,寫起來方便唄~

#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);
    }
        //沒有被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測試代碼

/* ** **** ******** **************** 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測試代碼 匯總

#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);
    }
        //沒有被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語言實現異常捕獲try-catch組件”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識,可以關注億速云行業資訊頻道,小編每天都會為大家更新不同的知識點。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

龙南县| 绥滨县| 三明市| 平塘县| 武山县| 故城县| 陵川县| 普兰县| 彭州市| 鹰潭市| 滕州市| 天长市| 城固县| 红安县| 麦盖提县| 工布江达县| 龙岩市| 西乡县| 石狮市| 宜城市| 策勒县| 抚顺县| 九龙坡区| 游戏| 广灵县| 辽宁省| 临城县| 乌拉特后旗| 山东省| 南阳市| 涡阳县| 山阴县| 隆子县| 凤庆县| 江安县| 象州县| 珲春市| 涿州市| 乐昌市| 金坛市| 中西区|