LogoLogo
AMPS C#/.NET Client 5.3.3
AMPS C#/.NET Client 5.3.3
  • Welcome to the AMPS C#/.NET Client
    • Before You Start
    • Obtaining and Installing the AMPS C#/.NET Client
    • Your First AMPS Program
      • Client Identification
      • Connection Strings for AMPS
      • Connection Parameters for AMPS
      • Providing Credentials to AMPS
      • Assembly Deployment
    • Subscriptions
      • Content Filtering
        • Changing the Filter on a Subscription
      • Understanding Message Objects
      • Synchronous Message Processing
      • Asynchronous Message Processing
        • Understanding Threading
      • Regular Expression Subscriptions
      • Ending Subscriptions
    • Error Handling
      • Exceptions
      • Exception Types
      • Exception Handling and Asynchronous Message Processing
      • Controlling Blocking with Command Timeout
      • Disconnect Handling
        • Using a Heartbeat to Detect Disconnection
        • Managing Disconnection
        • Replacing Disconnect Handling
      • Unexpected Messages
      • Unhandled Exceptions
      • Detecting Write Failures
      • Monitoring Connection State
    • State of the World
      • SOW and Subscribe
      • Setting Batch Size
      • Managing SOW Contents
      • Client Side Conflation
    • Using Queues
      • Backlog and Smart Pipelining
      • Acknowledging Messages
      • Acknowledgement Batching
      • Returning a Message to the Queue
      • Manual Acknowledgement
    • Delta Publish and Subscribe
      • Delta Subscribe
      • Delta Publish
    • High Availability
    • AMPS Programming: Working with Commands
    • Utility Classes
    • Advanced Topics
    • Exceptions Reference
    • AMPS Server Documentation
    • API Documentation
Powered by GitBook

Get Help

  • FAQ
  • Legacy Documentation
  • Support / Contact Us

Get AMPS

  • Evaluate
  • Develop

60East Resources

  • Website
  • Privacy Policy

Copyright 2013-2024 60East Technologies, Inc.

On this page
Export as PDF
  1. Welcome to the AMPS C#/.NET Client
  2. Error Handling

Unhandled Exceptions

In the AMPS C# client, exceptions can occur that are not thrown to the user. For example, when an exception occurs in the process of reading subscription data from the AMPS server, the exception occurs on a thread inside of AMPS. Consider the following example:

public class MyApp
{
    ...
    public static void WaitToBePoked(Client client)
    {
        client.subscribe(
            x=>Console.WriteLine("Hey! {0} poked you!", x.UserId),
            "pokes",
            string.Format("/Pokee LIKE '{0}-.*'", System.Environment.UserName),
            5000
        );
        Console.ReadKey();
    }
}

In this example, we set up a simple subscription to wait for messages on the pokes topic, whose Pokee tag begins with our username. When messages arrive, we print a message out to the console, but otherwise our application waits for a key to be pressed.

Inside of the AMPS client, the client creates a new thread of execution that reads data from the server, and invokes message handlers and disconnect handlers when those events occur. When exceptions occur inside this thread, however, there is no caller for them to be thrown to, and by default they are ignored.

In applications where it is important to deal with every issue that occurs in using AMPS, you can set an ExceptionListener via Client.setExceptionListener() that receives these otherwise unhandled exceptions. Making the modifications shown in the example below, to our previous example, will allow those exceptions to be caught and handled. In this case we are simply printing those caught exceptions out to the console.

If your application will attempt to recover from an exception thrown on the background processing thread, your application should set a flag and attempt recovery on a different thread than the thread that called the exception listener.

At the point that the AMPS client calls the exception listener, it has handled the exception. Your exception listener must not rethrow the exception (or wrap the exception and throw a different exception type).

public class MyApp
{
    ...
    public static void WaitToBePoked(Client client)
    {
        client.setExceptionListener(
            ex=>Console.Error.WriteLine(ex));
        client.subscribe(
            x=>Console.WriteLine("Hey! {0} poked you!",
                x.UserId),
            "pokes",
            string.Format("/Pokee LIKE '{0}-.*'",
                System.Environment.UserName),
            5000);
        Console.ReadKey();
    }
}

In this example we have added a call to client.setExceptionListener(), registering a simple function that writes the text of the exception out to the console. Even though our application waits for a user to press a key, messages to the console will still be produced, both as incoming poke messages arrive, and as issues arise inside of AMPS.

PreviousUnexpected MessagesNextDetecting Write Failures

Last updated 3 months ago