2

次のコードを取得しました。

set<Object*>::iterator it;
try
    {
        for (it = SetOfObjects->begin(); it != SetOfObjects->end(); ++it)
        {
            //some actions, not applicable to the question
        }
    }
    catch(...)
    {
        this->m_error_raiser->error_Name = "Station isn`t connected to Object! Use connectToObject method or list of forecast objects is empty";
        this->m_error_raiser->error_Number = 101;
        //throw (this->m_error_raiser);
    }

SetOfObjects のインスタンスが作成されず、そのセットを反復処理しようとすると、予想される実行時エラーが発生しました。

そのため、そのエラーを処理し、try catch を使用してユーザーに情報を提供することにしました。

私の質問:すべての例外をキャッチして処理されたと見なされますが、プログラムは実行時に終了しますが、これは私が期待する動作と矛盾します:生成されたすべての例外が処理されたため、引き続き動作するはずです。ここで何が問題なのですか?

4

2 に答える 2

3

オブジェクトがポインタで初期化されていない場合、そのようなオブジェクトの使用法は ですundefined behaviourexception handlingそのようなポインタの使用は(標準では)処理できません。デフォルトで 0 に初期化するだけで、ポインタがnull使用前にないことを確認します。

于 2012-08-14T10:28:51.257 に答える
2

In Windows environment you technically can catch low-level exceptions like this (dereferencing null/uninitialized pointer) - SEH exceptions. This is done by using Microsoft-specific __try() and __except() statements.

This may be useful, if you have an external not-so-well-written library, which crashes (follows null pointer, etc..) instead of reporting error i.e. when file is not found.

But, as already mentioned in comments, using this in your code is not portable. And they are not interoperable with C++ exceptions. So even if you decide to use them you'll end up with spagetti of 2 exception handling mechanisms... A bad design probably)

However, if youe code relies on exception-handling for error-reporting, you can always make a null check and throw a custom exception on failure: if(pointer==NULL) throw something;

于 2012-08-14T11:24:19.867 に答える