てみキャッチは、例外を処理する方法です。
// CATCH BLOCK
catch(MyException const& e)
{
// Correct a MyException
}
catch(std::exception const& e)
{
// Correct a std::exception
// For example this would cat any exception derived from std::exception
}
好きなだけ多くのキャッチブロックを配置できます。catch文のcatch式のいずれかと例外が一致した場合、関連するコードブロックが実行されます。キャッチ式が例外にマッチしない場合、スタックは上位のキャッチブロックを検出してプロセスが繰り返されるまで巻き戻されます(キャッチブロックが一致しないとアプリケーションが終了する可能性があります)。
注:複数のキャッチ式が一致すると、最初に字句的に使用されます。キャッチブロックは1つだけ実行されます。 noneの場合、コンパイラはより高いレベルのtry/catchを探します。
キャッチ何句は
catch(...)
{
// This is a catch all.
// If the exception is not listed above this will catch any exception.
}
は、どのように、これはあなたのコードには適用されないもあります。
int main()
{
char *ptr;
try
{
// This calls ::new() which can potentially throw std::bad_alloc
// If this happens then it will look for a catch block.
ptr = new char[ 1000000000 ];
// If the ::new() works then nothing happens and you pointer `ptr`
// is valid and code continues to execute.
}
catch(…)
{
// You only have one catch block that catches everything.
// So if there are any statements that generate an exception this will catch
// the excetption and execute this code.
cout << "Too many elements" << endl;
}
// As you have caught all exceptions the code will continue from here.
// Either after the try block finishes successfully or
// After an exception has been handled by the catch block.
return 0;
}
これはC++ [例外処理](http://www.parashift.com/c++-faq-lite/exceptions.html)です。別のリンクはここにあります:http://msdn.microsoft.com/en-us/library/6dekhbbc.aspx – birryree
これは多くの点で恐ろしい例です。あなたの目を洗い流し、文書を読んでください。 – bmargulies
FWIW C++を初めてお使いの方は例外処理と例外にはなりません – AJG85