?? ?? ???, ??? ??? ???? ?? ??? ?? ??? ?? ??? ???? ??? ?? ?? ???? ???? C++? ?? ?? ??? ??? ? ????. ?? ?? ??? ????? std::Exception?? ??? ?? ???? ?????. ??? ?? ??? ?????? throw ???? ??????. try-catch ???? Dynamic_cast? ???? ??? ??? ??? ?? ?? ???? ?????. ?? ?? open_file ??? FileNotFoundException ??? ??????. ??? ???? ???? ?? ???? ?? ???? ??? ? ????.
C++ ?? ?? ??: ??? ?? ?? ??
?? ??? ?? ????? ???? ?? ? ??? ???? ??? ???????. C++??? ????? try-catch
??? ???? ??? ???? ?????. ??? ?? ?? ??(?: std::Exception
)? ??? ??? ????? ??? ? ?? ??? ??? ? ????. try-catch
塊來捕獲和處理。然而,標準異常類型 (例如 std::exception
) 只提供有限的信息,這可能會給調(diào)試和錯誤處理帶來困難。
定制異常類
為了創(chuàng)建更具信息性和可操作性的異常,你可以定義自己的異常類。這樣做的好處包括:
- 提供特定的錯誤消息
- 包含附加上下文信息(例如行號)
- 根據(jù)錯誤類型執(zhí)行自定義操作
要定義異常類,只需要創(chuàng)建一個繼承自 std::exception
的類:
class MyException : public std::exception { public: explicit MyException(const std::string& message) : message(message) {} const char* what() const noexcept override { return message.c_str(); } private: std::string message; };
使用異常類型
在使用定制異常類時,你可以通過 throw
關(guān)鍵字拋出它們:
throw MyException("Error occurred during file operation");
在 try-catch
塊中,可以使用 dynamic_cast
將捕獲到的異常轉(zhuǎn)換為定制異常類型:
try { // 代碼可能引發(fā)異常 } catch (std::exception& e) { std::cerr << "Standard exception: " << e.what() << std::endl; } catch (MyException& e) { std::cerr << "MyException: " << e.what() << std::endl; }
實戰(zhàn)案例
假設(shè)有一個函數(shù) open_file
,用于打開一個文件。如果文件不存在或無法打開,它將拋出一個 FileNotFoundException
異常:
class FileNotFoundException : public std::exception { public: explicit FileNotFoundException(const std::string& filename) : filename(filename) {} const char* what() const noexcept override { return ("File not found: " + filename).c_str(); } private: std::string filename; }; std::ifstream open_file(const std::string& filename) { std::ifstream file(filename); if (!file.is_open()) { throw FileNotFoundException(filename); } return file; }
在調(diào)用 open_file
函數(shù)時,你可以使用 try-catch
塊來捕獲并處理 FileNotFoundException
??? ?? ?? ???
????? ???? ?? ??? ??? ???? ??? ?? ???? ??? ? ????. ?? ?? ??? ??? ????: ??- ?? ?? ??? ??
- ?? ??? ??(?: ? ??) ??
- ?? ??? ?? ??? ?? ?? ?? li>
- li>
std::Exception
?? ???? ???? ?????. ??try { std::ifstream file = open_file("myfile.txt"); // 使用文件 } catch (FileNotFoundException& e) { std::cerr << "File not found: " << e.what() << std::endl; } catch (std::exception& e) { std::cerr << "Other error: " << e.what() << std::endl; }?????? ?? ???????? ??? ?? ?? ???? ??
throw
???? ?? ?? ? ????: ??rrreee?? try-catch
???? dynamic_cast
? ???? ??? ??? ??? ??? ?? ???? ?????: ??rrreee?????? ??????????? ?? ? ???? open_file
??? ??? ?????. ??? ??? ? ? ?? ?? FileNotFoundException
??? ?????. ??rrreee?? open_file
??? ??? ? try-catch? ??? ? ????.
code> ??? ???? FileNotFoundException
? ???? ?????. ??rrreee?? ??? ?? ??? ? ?? ??? ??? ?? ?? ???? ?? ???? ??? ? ????. ??? ??? C++ ?? ?? ??: ??? ??? ?? ??? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

PHP? AI ??? ?? ??? ??? ?? AI ??? API (? : OpenAI, HuggingFace)? ?????? ???? ??? ???, API ??, ?? ?? ? ?? ?????? ???? ????. 2. ??? ??? ??? ??? AI ???? ???? ????. ?? ??? API, ??? ?? ? ??? ??? ???? ????. 3. ?? ??? ?? ??, ??, ??, ???, ??? ????? ? GPT ?? BART/T5? ?? ?? ??? ???????. 4. ?? ????? ??, ??? ?, ?? ?? ? ?? ?? ??? ?????. ?? ??? ???? ????? ???? ??? ???? ?? ?? ?? ? ??, ???? ?? ??, ?? ??, ?? ?? ? ??? ???????.

std :: ??? ? ?? ??? ?? 4 ?? ???? ??? ????. 1. ?? () ???? ???? ??? ?? ?? ??? ??? ??? ?? ?? ???? ?? ????. 2. ?? [0]? ???? Front ()? ??? ? ??? ??? ?? ?? ??? ????????. 3. ?? ????? ? STL ????? ??? *?? () ??; 4. ???? ????? ?? ??? ?? ?? (0)? ???? ??? ?? ? ??? ?????. ?? ??? ?? ?? ??? ?????. ?? ??? ?? ? ()? ???? ?? ??? ??? ?? Front () ???? ???? ???? ?? ??? ??? ?? ? ?? ??? ?? ????.

?? ?? ??? ?? ??? ????? ??? ? ????. 1. I-th ??? 1?? ?????? : N & (1 ??

C ?? ?????? ???? ??? ???? ???? ?? ??? ???? ? ??? ?????. 1. STL ????? ?? ??? ??? ??, ??? ?? ? ??? ??? ??? ?? ??? ?? ??????, UNOrdered_map? ?? ??? ?????. 2. ??, ?? ? ??? ?? ?? ????? ????? ???? ????? ??? ?? ? ????. 3. ??? ??? ??? _ptr ? shared_ptr? ??? ??? ?? ???? ????? ?????. 4. ??, ?? ? ??? ?? ?? ??? ?? ?? ? ???? ??????. ??? ?? ??? ????? ?? ???? ?? ??? ?? ??? ? ? ????.

??? ?? ??? ? ???? ???? ? ???? C? ??? ???? ?? ?????. 1. ??? Intadd (Inta, Intb)? ?? ?? ? ??? ?? ?????. 2. ??? ?? ? ? ?? ??? ???? ??? ?? ? ? ?? ??? ??? ?????. 3. ?? ???? ??? ??? ??? ???? ?? voidGreet (StringName)? ?? ?? ???? void? ?????. 4. ??? ???? ?? ???? ?????, ??? ???, ?? ??? ???? ? ? ???, ?? C ?????? ?? ?????.

C ABI? ????? ?? ??? ?? ? ? ??? ?? ????, ?? ??, ?? ????, ?? ?? ?? ?? ????? ?????. 1. ??? ??? ??? ???? ?? ??? ? ??? ?????. 2. ?? ???? ?? ??? ?? ????? ??, STL ??, ??? ??, ???? ??? ?? ? ??? ABI? ?? ? ? ????. ?? ? ?? ????? ABI ???? ?????? ???????.

STD :: IS_SAME? ??? ???? ? ??? ??? ??? ? ??? ???? ?? ?? ???? ? ?????. 1. ?? ???? std :: is_same :: ?? t? u? ??? ?? ? ? ?? true???. ??? ??? ?????. const, reference, pointer ?? ?? ?? ???? ??? ????. 2. std :: remove_const, std :: remove_reference ? ?? ???? ?? ??? ??? ?? ?????? ??? ?? ??? ?? ? ? ????. 3. IFConstexpr? ??? ??? ??? ?? ?? ?? ???? ??? ?? ?? ??? ?? ???? ?? ??? ?? ?? ??? ?????. 4.

decltype? ??? ??? ?? ??? ???? ?? C 11?? ???? ??????. ?? ??? ???? ?? ??? ???? ????. 1. decltype (expression) ? ??? ???? ??? ???? ????. 2. ?? ?? decltype (x)? ?? ???? ???? ??, decltype ((x))? lvalue ???? ?? X? ?????. 3. ????? ?? ?? ?? ?? ?? AUTO-> DECTYPE (t u)? ?? ?? ?? ???? ? ?????. 4. ??? ?? ??? decltype (vec.begin ())? ?? ???? ???? ??? ? ? ????. it = vec.begin (); 5. ????? ?? ?? ???? ?????
