LogoLogo
AMPS Java Client 5.3.4
AMPS Java Client 5.3.4
  • Welcome to the AMPS Java Client
    • Before You Start
    • Obtaining and Installing the AMPS Java Client
    • Your First AMPS Program
      • Client Identification
      • Connection Strings for AMPS
      • Connection Parameters for AMPS
      • Providing Credentials to AMPS
    • 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
      • Acknowledgment Batching
      • Returning a Message to the Queue
      • Manual Acknowledgment
      • Samples of Using a Queue
    • Delta Publish and Subscribe
      • Delta Subscribe
      • Delta Pubilsh
    • 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 Java Client
  2. Error Handling

Exceptions

Generally speaking, when an error occurs that prohibits an operation from succeeding, AMPS will throw an exception. AMPS exceptions universally derive from com.crankuptheamps.client.exception.AMPSException, so by catching AMPSException, you will be sure to catch anything AMPS throws. For example:

import com.crankuptheamps.client.Client;
import com.crankuptheamps.client.exception.AMPSException;

public void readAndEvaluate(Client client) {
    Scanner scanner = new Scanner(System.in);
    String payload = scanner.nextLine();

    // write a new message to AMPS
    if ( payload != null) {
        try {
            client.publish("UserMessage", " { \"data\" : \"" + payload + "\" }");
        }
        catch (AMPSException e) {
            System.err.println("An AMPS exception occurred: " + e.toString());
            e.printStackTrace();
        }
    }
}

In this example, if an error occurs, the program writes the error to stderr and the publish() command fails. However, client is still usable for continued publishing and subscribing. When the error occurs, the exception is written to stderr, which calls the exception's toString() method. As with most Java exceptions, toString() will convert the Exception into a string that includes a message. The printStackTrace() method will write to stderr the stack trace and information on any ”inner” exceptions (exceptions from outside of AMPS that caused AMPS to throw an exception).

AMPS exception types vary based on the nature of the error that occurs. In your program, if you would like to handle certain kinds of errors differently than others, you can catch the appropriate subclass of AMPSException to detect those specific errors and do something different.

public CommandId CreateNewSubscription(Client client) {
    CommandId id = null;
    string topicName;
    while (id == null) {
        /* Our program is an interactive program that attempts to retrieve a topic
         * name (or regular expression) from the user.
         */
        topicName = askUserForTopicName();
        try {
            Command command = new Command("subscribe").setTopic(topicName);
            MessagePrinter mp = new MessagePrinter();
            id = client.executeAsync(command, mp);
        }
        catch(BadRegexTopicException ex) {
            /* This line indicates that the program catches the BadRegexTopicException
             * exception and displays a specific error to the user indicating the topic
             * name or expression was invalid. By not returning from the function in
             * this catch block, the while loop runs again and the user is asked for
             * another topic name.
             */
            System.err.println("Error: bad topic name " +
                "or regular expression " + topicName +
                ". The error was: " + ex.toString());
            // we’ll ask the user for another topic
        }
        catch(AMPSException ex) {
        /* If an AMPS exception of a type other than BadRegexTopicException is thrown
         * by AMPS, it is caught here. In that case, the program emits a different
         * error message to the user.
         */
            System.err.println("Error: error setting " +
                "up subscription to topic " +
                topicName + ". The error was: " +
                ex.toString());

            return null; // give up
        }
    }
    return id;
}
PreviousError HandlingNextException Types

Last updated 3 months ago