2015年5月25日月曜日

stdexcept(C++)

stdexceptは標準的な例外クラスを提供する。

これらのクラス群に反映されているエラーモデルの種類
  • runtime error 実行時エラー
  • logic error 論理エラー
実行時エラーは、プログラムの制御の及ばない事象を起因とする。
論理エラーは、プログラムの内部的論理の誤りに起因する。

以下の様なクラスが定義されている。
  • exception
    • runtime error
      • range_error
      • overflow_error
      • underflow_error
    • logic error
      • domain_error
      • invalide_argument
      • length_error
      • out_of_range
これらのクラスは同じpublic関数が定義されており、ジェネリックな呼び出しが可能である。
explicit T(const string& what_arg){} 指定したメッセージをもつ例外オブジェクトの生成 
explicit T(const char* what_arg){} C++11 指定したメッセージをもつ例外オブジェクトの生成
virtual const char* what() const noexcept; メッセージの取得

//
// stdexcept.cpp
// CplusplusPractice
//
// Created by masai on 2015/05/25.
// Copyright (c) 2015年 masai. All rights reserved.
//
#include <iostream>
#include <stdexcept>
using namespace std;
class processing{
public:
void function(){
throw std::logic_error("an logic error.");
}
};
int main(int argc, char* argv[]){
processing p;
try{
p.function();
}catch(const std::logic_error& e){
cout << e.what() <<endl;
}
}
view raw stdexcept.cpp hosted with ❤ by GitHub

0 件のコメント:

コメントを投稿