This topic describes how to handle errors in a Java runtime environment, including error types and exception information.

Error types

  • If an error occurs when a function is being executed, Function Compute captures the error and returns an error message.

    Sample code:

    package example;
    
    import com.aliyun.fc.runtime.Context;
    import com.aliyun.fc.runtime.StreamRequestHandler;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    
    public class HelloFC implements StreamRequestHandler {
    
        @Override
        public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
            throw new IOException("oops");
        }
    } 
    When the function is invoked, the following response is returned:
    {
      "errorMessage" : "oops",
      "errorType" : "java.io.IOException",
      "errorCause" : "oops",
      "stackTrace" : [ "example.HelloFC.handleRequest(HelloFC.java:15)" ]
    }
  • If your function proactively exits while it is running, the system returns a general error message.

    Sample code:

    package example;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    
    import com.aliyun.fc.runtime.Context;
    import com.aliyun.fc.runtime.StreamRequestHandler;
    
    public class App implements StreamRequestHandler {
    
        @Override
        public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
            System.exit(-1);
        }
    }
    When the function is invoked, the following response is returned:
    {
      errorMessage: 'Process exited unexpectedly before completing request (duration: 43ms, maxMemoryUsage: 65MB)'
    }

Error information

The following table describes the fields in the error information.
Field Category Description
errorMessage String The error message.
errorType String The error type.
stackTrace List The error stack.

For more error types, see Error handling.