![](/img/trans.png)
[英]Error with catching std::runtime_error as std::exception
[英]A wstring wrapper to std::runtime_error exception?
假設我在某處定義了以下功能:
std::string ws_to_s(const wchar_t*);
std::string ws_to_s(const std::wstring&);
std::wstring s_to_ws(const char*);
我寫了這個課:
class runtime_error_w : public std::runtime_error {
public:
runtime_error_w() = default;
runtime_error_w(const wchar_t* what_arg)
: runtime_error(ws_to_s(what_arg)) { }
runtime_error_w(const std::wstring& what_arg)
: runtime_error(ws_to_s(what_arg)) { }
virtual const std::wstring what_w() const noexcept {
return ws_to_s(what());
}
};
到目前為止,這對我有用,但是這種方法有什么問題嗎?
注意, what_w()
返回一個std::string
對象,而不是const char*
。
基於問題下方的注釋,我同意使用此類是一個壞主意 ,因為在構造wstring
時可能會引發bad_alloc
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.