AWS Lambda
Developer Guide

Function Errors (Java)

If your Lambda function throws an exception, AWS Lambda recognizes the failure and serializes the exception information into JSON and returns it. Following is an example error message:

{ "errorMessage": "Name John Doe is invalid. Exception occurred...", "errorType": "java.lang.Exception", "stackTrace": [ "example.Hello.handler(", "sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)", "sun.reflect.NativeMethodAccessorImpl.invoke(", "sun.reflect.DelegatingMethodAccessorImpl.invoke(", "java.lang.reflect.Method.invoke(" ] }

Note that the stack trace is returned as the stackTrace JSON array of stack trace elements.

The method in which you get the error information back depends on the invocation type that you specified at the time you invoked the function:

  • RequestResponse invocation type (that is, synchronous execution): In this case, you get the error message back.

    For example, if you invoke a Lambda function using the Lambda console, the RequestResponse is always the invocation type and the console displays the error information returned by AWS Lambda in the Execution result section as shown in the following image.

  • Event invocation type (that is, asynchronous execution): In this case AWS Lambda does not return anything. Instead, it logs the error information in CloudWatch Logs and CloudWatch metrics.

Depending on the event source, AWS Lambda may retry the failed Lambda function. For example, if Amazon Kinesis is the event source for the Lambda function, AWS Lambda retries the failed function until the Lambda function succeeds or the records in the stream expire.

Function Error Handling

You can create custom error handling to raise an exception directly from your Lambda function and handle it directly (Retry or Catch) within an AWS Step Functions State Machine. For more information, see Handling Error Conditions Using a State Machine.

Consider a CreateAccount state is a task that writes a customer's details to a database using a Lambda function.

  • If the task succeeds, an account is created and a welcome email is sent.

  • If a user tries to create an account for a username that already exists, the Lambda function raises an error, causing the state machine to suggest a different username and to retry the account-creation process.

The following code samples demonstrate how to do this. Note that custom errors in Java must extend the Exception class.

package com.example; public static class AccountAlreadyExistsException extends Exception { public AccountAlreadyExistsException(String message) { super(message); } } package com.example; import; public class Handler { public static void CreateAccount(String name, Context context) throws AccountAlreadyExistsException { throw new AccountAlreadyExistsException ("Account is in use!"); } }

You can configure Step Functions to catch the error using a Catch rule. Lambda automatically sets the error name to the fully-qualified class name of the exception at runtime:

{ "StartAt": "CreateAccount", "States": { "CreateAccount": { "Type": "Task", "Resource": "arn:aws:lambda:us-east-1:123456789012:function:CreateAccount", "Next": "SendWelcomeEmail", "Catch": [ { "ErrorEquals": ["com.example.AccountAlreadyExistsException"], "Next": "SuggestAccountName" } ] }, … } }

At runtime, AWS Step Functions catches the error, transitioning to the SuggestAccountName state as specified in the Next transition.

Custom error handling makes it easier to create serverless applications. This feature integrates with all the languages supported by the Lambda Programming Model, allowing you to design your application in the programming languages of your choice, mixing and matching as you go.

To learn more about creating your own serverless applications using AWS Step Functions and AWS Lambda, see AWS Step Functions.