例外処理の概要 (Windows Server AppFabric キャッシュ)

Windows Server AppFabric のキャッシュ API は、エラーが発生すると .NET 例外をスローします。このトピックでは、エラー処理の基本的な概念を説明し、例を示します。

DataCacheException クラス

AppFabric のキャッシュ メソッドに固有のエラーに対しては、共通の DataCacheException オブジェクトがスローされます。DataCacheException オブジェクトには、例外の原因の診断に役立つ 4 つのプロパティが含まれます。

DataCacheException のプロパティ 説明

Message

エラーについて説明している文字列。

ErrorCode

DataCacheErrorCode クラスでのエラー コード定数に対応する整数値。

SubStatus

DataCacheErrorSubStatus クラスでのサブステータス定数に対応する整数値。

InnerException

現在の例外の原因になった例外インスタンス。この値は null の場合があります。

タイムアウトなどの一部の障害は、キャッシュ クライアント メソッドから発生する場合があります。アプリケーションのコードはこれらの一般的な例外を処理できるようになっている必要があります。詳細については、「一般的例外 (Windows Server AppFabric キャッシュ)」を参照してください。

ヒント

障害の中には例外をスローしないものもあります。たとえば、Get メソッドは、キーが見つからない場合は null 値を返します。他のメソッドは、成功または失敗を示すブール値を返す場合があります。特定のメソッドの詳細については、Windows Server AppFabric クラス ライブラリのドキュメントで Microsoft.ApplicationServer.Caching 名前空間を参照してください。

次の例では、strObject という名前の文字列オブジェクトを、myCache という名前の DataCache に格納しようとしています。オーバーロードされた Put メソッドを使用して、オブジェクトに対するキャッシュ領域を指定します。この領域がキャッシュにまだ存在しない場合、エラー コード RegionDoesNotExistDataCacheException オブジェクトがスローされます。この例では、領域を作成して格納操作を再試行することでエラーを処理しています。

Dim strKey As String = "key0"
Dim strObject As String = "Source String"

Try
   ' Put a string object into the cache region, "Region1"
   myCache.Put(strKey, strObject, "Region1")

Catch cacheError As DataCacheException
   ' Look at the ErrorCode property to see if the Region is missing
   If (cacheError.ErrorCode = DataCacheErrorCode.RegionDoesNotExist) Then

      ' Create the Region and retry the Put call
      myCache.CreateRegion("Region1")
      myCache.Put(strKey, strObject, "Region1")
   End If
End Try
string strKey = "key0";
string strObject = "Source String";

try
{
   // Put a string object into the cache region, "Region1"
   myCache.Put(strKey, strObject, "Region1");
}
catch (DataCacheException cacheError)
{
   // Look at the ErrorCode property to see if the Region is missing
   if (cacheError.ErrorCode == DataCacheErrorCode.RegionDoesNotExist)
   {
      // Create the Region and retry the Put call
      myCache.CreateRegion("Region1");
      myCache.Put(strKey, strObject, "Region1");
   }
}

関連項目

概念

一般的例外 (Windows Server AppFabric キャッシュ)
キャッシュ クライアントのタイムアウトを構成する (Windows Server AppFabric キャッシュ)

  2011-12-05