Using Exception Handling in Web Applications

Exception handlers are designed for those Windows Media Center APIs that can throw exceptions. The web application can manage these exceptions without allowing the application to crash. Exception handlers should only be used with Windows Media Center web applications (never in local applications with an installed managed-code assembly).

The following example shows how to set up exception handlers for the ApplicationContext.RegisterApplication method. When the method is called, rules determine whether an exception was thrown.

    <Locals>
      <mce:AddInHost Name="WMCHost"/>
      <ExceptionHandler Name="MyExceptionHandler"/>
      <Command Name="MyCommand" Description="Register Application" />
    </Locals>

    <Rules>
      <Changed Source="[MyCommand.Invoked]">
        <Actions>
          <Invoke Target="[WMCHost.ApplicationContext.RegisterApplication]"
                  uri="http://play.mediacentersandbox.com/sample/6/SamplerRegistration.xml"
                  unRegister="false" allUsers="false">
            <HandleExceptions>
              <HandleException ExceptionType="Microsoft.MediaCenter.ApplicationAlreadyRegisteredException"
                               ExceptionHandler="[MyExceptionHandler]" />
              <HandleException ExceptionType="Microsoft.MediaCenter.ApplicationNotRegisteredException"
                               ExceptionHandler="[MyExceptionHandler]" />
              <HandleException ExceptionType="Microsoft.MediaCenter.ApplicationNoPermissionException"
                               ExceptionHandler="[MyExceptionHandler]" />
              <HandleException ExceptionType="Microsoft.MediaCenter.ApplicationRegistrationCancelledException"
                               ExceptionHandler="[MyExceptionHandler]" />
            </HandleExceptions>
          </Invoke>
        </Actions>
      </Changed>
      <Changed Source="[MyExceptionHandler.Caught]">
        <Actions>
            <!-- To Do: set up actions to handle the exception -->
        </Actions>
      </Changed>
    </Rules>

Sample Explorer

  • Web > Exception Handler

See Also