Yandex Cloud
Search
Contact UsGet started
  • Blog
  • Pricing
  • Documentation
  • All Services
  • System Status
    • Featured
    • Infrastructure & Network
    • Data Platform
    • Containers
    • Developer tools
    • Serverless
    • Security
    • Monitoring & Resources
    • ML & AI
    • Business tools
  • All Solutions
    • By industry
    • By use case
    • Economics and Pricing
    • Security
    • Technical Support
    • Customer Stories
    • Gateway to Russia
    • Cloud for Startups
    • Education and Science
  • Blog
  • Pricing
  • Documentation
Yandex project
© 2025 Yandex.Cloud LLC
Yandex Cloud Functions
  • Comparison with other Yandex Cloud services
    • Overview
    • Managing dependencies
    • Request handler
    • Invocation context
    • Logging
    • Handling errors
    • Using the SDK
  • Tools
  • Pricing policy
  • Access management
  • Terraform reference
  • Monitoring metrics
  • Audit Trails events
  • Release notes
  • FAQ
  1. Developing in Java
  2. Handling errors

Java function error handling

Written by
Yandex Cloud
Updated at August 22, 2024

If a handler reports a function runtime or loading error, the runtime environment automatically captures the error and returns a JSON document with the error type in the response. For more information about the JSON document format, see Calling a function. Error info is written to the execution log.

Examples of error handlingExamples of error handling

Case 1: User code goes outside the array boundaries, resulting in ArrayIndexOutOfBoundsException being thrown. The runtime environment captures the exception and generates a JSON document that contains an error message (the errorMessage field), error type (the errorType field), and stack trace (the stackTrace field).

Function code:

import java.util.function.Function;

public class Handler implements Function<byte[], Integer> {
  @Override
  public Integer apply(byte[] input) {
    final var array = new int[]{1, 2, 3, 4, 5};
    // at this point, the function throws an ArrayIndexOutOfBoundsException
    return array[15];
  }
}

JSON document returned:

{
  "errorMessage": "Index 15 out of bounds for length 5",
  "errorType": "ArrayIndexOutOfBoundsException",
  "stackTrace": [
    ...
  ]
}

Case 2: User code indicates an error by throwing an exception from the function.

Function code:

import java.util.function.Function;

public class Handler implements Function<byte[], Integer> {
  @Override
  public Integer apply(byte[] input) {
    throw new RuntimeException("Some error message");
  }
}

JSON document returned:

{
  "errorMessage": "Some error message",
  "errorType": "RuntimeException",
  "stackTrace": [
    ...
  ]
}

Was the article helpful?

Previous
Logging
Next
Using the SDK
Yandex project
© 2025 Yandex.Cloud LLC