User Tools

Site Tools


programming:cpp:exception

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
programming:cpp:exception [2022/05/05 07:30]
odagawa
programming:cpp:exception [2022/05/05 11:09] (current)
odagawa
Line 1: Line 1:
 =====例外処理===== =====例外処理=====
 +
 +try-catch 文を用いることでプログラムのどこでエラーが起こっているかをスムーズに把握することができる.\\
 +''try'' で例外処理が発生しうる場合を囲み, ''catch'' で例外クラスを把握して確認する,という流れ.\\
 +例外クラスは ''throw'' を用いて投げる.
  
 STL で用意されている例外クラスは以下の通り STL で用意されている例外クラスは以下の通り
Line 14: Line 18:
 int main (int argc, char *argv[]) { int main (int argc, char *argv[]) {
  
-try {+  try {
  
-if (/*例外条件*/)+  if (/*例外条件*/)
     throw std::runtime_error("Runtime error : " << "内容");     throw std::runtime_error("Runtime error : " << "内容");
  
-} catch ( std::runtime_error &error ) { +  } catch ( const std::runtime_error &error ) { 
-    std::cerr << error << std::endl;+    std::cerr << error.what() << std::endl; 
 +    std::exit(1); 
 +  } catch ( const std::invalid_argument &error ) { 
 +    std::cerr << error.what() << std::endl;
     std::exit(1);     std::exit(1);
-}+  }
  
-std::exit(0);+  std::exit(0);
  
 } }
 </code> </code>
programming/cpp/exception.1651735801.txt.gz · Last modified: 2022/05/05 07:30 by odagawa