メインコンテンツ

MISRA C++:2008 Rule 15-5-1

A class destructor shall not exit with an exception.

説明

ルール定義

A class destructor shall not exit with an exception. 1

Polyspace 実装

チェッカーは、デストラクター本体内でスローされる例外にフラグを設定します。デストラクターで別の関数が呼び出されていても、チェッカーはその関数で例外がスローされているかどうかを検出しません。

チェッカーは以下の状況を検出しません。

  • catch ステートメントで、スローされているすべての型の例外がキャッチされていない。

    チェッカーは、try ブロックに対応する catch ステートメントの存在を、例外がキャッチされていることを示すものだと見なします。

  • throw ステートメントが catch ブロック内にある。

トラブルシューティング

ルール違反が想定されるものの、Polyspace® から報告されない場合は、コーディング規約違反が想定どおりに表示されない理由の診断を参照してください。

すべて展開する

この例では、クラス myClass のデストラクターに、try-catch ブロックで囲まれていない明示的な throw ステートメントが含まれています。Polyspace はこの throw ステートメントにフラグを設定します。

#include <exception>
#include <stdexcept>
class myClass {
	//...
	~myClass()  { 
		//...
        if(cond)
            throw std::logic_error("Error"); //Noncompliant
	}
	private:
    volatile int cond;
};

チェック情報

グループ: Exception Handling
カテゴリ: 必要

バージョン履歴

R2013b で導入


1 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.

The MISRA coding standards referenced in the Polyspace Bug Finder™ documentation are from the following MISRA standards:

  • MISRA C:2004

  • MISRA C:2012

  • MISRA C:2023

  • MISRA C++:2008

  • MISRA C++:2023

MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.