exception類的實現

2021-06-26 02:50:08 字數 3428 閱讀 5582



1) 函式後面宣告 throw() 只是介面的提供者和介面的使用者間的默契或稱協議。

2) 這種協議不影響正常的異常處理流程。

throw()表示此函式不會丟擲異常,函式後面可以跟上 throw( int ),表示該函式可能會丟擲 int型的異常。但不會丟擲別的型別的異常。使用者應該注意捕獲 該函式可能丟擲的int型的異常

c++的異常類是沒有棧痕跡的,如果需要獲取棧痕跡,需要使用以下函式:

#include int backtrace(void **buffer, int

size);

char **backtrace_symbols(void *const *buffer, int

size);

void backtrace_symbols_fd(void *const *buffer, int size, int fd);

backtrace將當前程式的呼叫資訊儲存在buffer中,backtrace_symbols則是將buffer翻譯為字串。後者用到了malloc,所以需要手工釋放記憶體。

man手冊中提供了如下的**:

#include #include 

#include

#include

void

myfunc3(

void

)

for (j = 0; j < nptrs; j++)

printf(

"%s\n

", strings[j]);

free(strings);

}static

void

/*"static" means don't export the symbol...

*/myfunc2(

void

)void

myfunc(

intncalls)

intmain(

int argc, char *argv)

myfunc(atoi(argv[

1]));

exit(exit_success);

}

編譯並執行:

$  cc -rdynamic prog.c -o prog

$ ./prog 3

輸出如下:

backtrace() returned 8

addresses

./prog(myfunc3+0x1f) [0x8048783]

./prog() [0x8048810]

./prog(myfunc+0x21) [0x8048833]

./prog(myfunc+0x1a) [0x804882c]

./prog(myfunc+0x1a) [0x804882c]

./prog(main+0x52) [0x8048887]

/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3) [0xb76174d3]

./prog() [0x80486d1]

因此我寫出以下的異常類,注意上面的列印結果經過了名字改編,所以我們使用abi::__cxa_demangle將名字還原出來。

exception.h

#ifndef exception_h_

#define exception_h_#include

#include

class exception : public

std::exception

;#endif

//exception_h_

exception.cpp

#include "

exception.h

"#include

#include

#include

#include

using

namespace

std;

exception::exception(

const

char*msg)

: message_(msg)

exception::exception(

const

string&msg)

: message_(msg)

exception::~exception() throw

()const

char* exception::what() const

throw

()const

char* exception::stacktrace() const

throw

()//

填充棧痕跡

void

exception::fillstacktrace()

free(strings);

}}//

反名字改編

string exception::demangle(const

char*symbol)

}//if that didn't work, try to get a regular c symbol

if (1 == sscanf(symbol, "

%127s

", temp))

//if all else fails, just return the symbol

return

symbol;

}

測試**如下:

#include "

exception.h

"#include

using

namespace

std;

class

bar};

void

foo()

intmain()

catch (const exception&ex)

}

列印結果如下:

reason: oops

stack trace: exception::fillstacktrace()

exception::exception(

char const*)

bar::test()

foo()

./a.out(main+0xf)

/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)

./a.out()

注意編譯的時候,加上-rdynamic選項

有了這個類,我們可以在程式中這樣處理異常:

try

catch (const exception&ex)

catch (const std::exception&ex)

catch

(...)

Error類和Exception類的區別

error類和exception類的父類都是throwable類,他們的區別是 error類一般是指與虛擬機器相關的問題,如系統崩潰,虛擬機器錯誤,記憶體空間不足,方法呼叫棧溢等。對於這類錯誤的導致的應用程式中斷,僅靠程式本身無法恢復和和預防,遇到這樣的錯誤,建議讓程式終止。exception類表示...

C 異常處理 三 exception類

1,exception 標頭檔案 include c 可以把它用作其它異常類的基類。可以引發exception異常,也可以把exception用作基類,在從exception派生而來的類中重新定義乙個名為what 的虛擬成員函式,它返回乙個字串,該字串隨實現而異。include class bad ...

對於Exception的處理

使用checked exception還是unchecked exception的原則,我的看法是根據需求而定。如果你希望強制你的類呼叫者來處理異常,那麼就用checked exception 如果你不希望強制你的類呼叫者來處理異常,就用unchecked。那麼究竟強制還是不強制,權衡的依據在於從業...