Jaa


Exception Handling Overview

The caching API of Microsoft AppFabric 1.1 for Windows Server throws .NET exceptions when errors occur. This topic reviews basic error handling concepts and provides an example.

The DataCacheException Class

A common DataCacheException object is thrown for errors that are specific to the AppFabric Caching methods. The DataCacheException object contains four properties that can help you diagnose the cause of the exception:

DataCacheException property Description

Message

A string that describes the error.

ErrorCode

An integer value that corresponds to an error code constant in the DataCacheErrorCode class.

SubStatus

An integer value that corresponds to a sub-status constant in the DataCacheErrorSubStatus class.

InnerException

An exception instance that caused the current exception. This value can be null.

Some failures, such as a timeout, can occur from any cache client method. Your application code should be prepared to handle these common exceptions. For more information, see Common Exceptions.

Note

Some failures do not throw an exception. For example, the Get method returns a null value if the key is not found. Other methods can return Boolean values to indicate success or failure. For more information about specific methods, see the Microsoft.ApplicationServer.Caching namespace in the Microsoft AppFabric 1.1 for Windows Server Class Library documentation.

Example

The following example attempts to put a string object named strObject into a DataCache named myCache. The overloaded Put method is used to specify a cache region for the object. If this region does not yet exist in the cache, a DataCacheException object is thrown with an error code of RegionDoesNotExist. In this example, this error is handled by creating the region and retrying the put operation.

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");
   }
}

See Also

Concepts

Common Exceptions
Configuring Cache Client Timeouts

  2012-09-12