Errors in Minitab

import java.util.Arrays;

public class ErrorHandling {

    // In Java, we use exceptions for error handling instead of
    // returning error values. We'll define a custom exception.
    public static class CustomException extends Exception {
        public CustomException(String message) {
            super(message);
        }
    }

    // This method throws an exception instead of returning an error
    public static int f(int arg) throws CustomException {
        if (arg == 42) {
            // In Java, we throw exceptions instead of returning them
            throw new CustomException("can't work with 42");
        }
        // If no exception is thrown, the operation was successful
        return arg + 3;
    }

    // Sentinel errors in Java are typically implemented as static final fields
    public static final CustomException ERR_OUT_OF_TEA = new CustomException("no more tea available");
    public static final CustomException ERR_POWER = new CustomException("can't boil water");

    public static void makeTea(int arg) throws CustomException {
        if (arg == 2) {
            throw ERR_OUT_OF_TEA;
        } else if (arg == 4) {
            // In Java, we can't wrap exceptions like in Go,
            // but we can create a new exception with the cause
            throw new CustomException("making tea: " + ERR_POWER.getMessage());
        }
    }

    public static void main(String[] args) {
        for (int i : Arrays.asList(7, 42)) {
            try {
                int r = f(i);
                System.out.println("f worked: " + r);
            } catch (CustomException e) {
                System.out.println("f failed: " + e.getMessage());
            }
        }

        for (int i = 0; i < 5; i++) {
            try {
                makeTea(i);
                System.out.println("Tea is ready!");
            } catch (CustomException e) {
                // In Java, we use instanceof to check the type of an exception
                if (e == ERR_OUT_OF_TEA) {
                    System.out.println("We should buy new tea!");
                } else if (e.getMessage().contains(ERR_POWER.getMessage())) {
                    System.out.println("Now it is dark.");
                } else {
                    System.out.println("unknown error: " + e.getMessage());
                }
            }
        }
    }
}

In Java, error handling is typically done using exceptions rather than returning error values. Here’s a breakdown of the key differences:

  1. Instead of returning an error, Java methods throw exceptions.

  2. The try-catch block is used to handle exceptions, similar to how Go uses if err != nil.

  3. Java doesn’t have built-in support for error wrapping like Go’s fmt.Errorf with %w. Instead, we can create new exceptions with the cause specified.

  4. Java doesn’t have an exact equivalent to Go’s errors.Is. We can use == for comparing singleton exceptions, or instanceof for checking exception types.

  5. Sentinel errors in Java are typically implemented as static final fields of exception objects.

This Java code mimics the behavior of the Go example as closely as possible, demonstrating error handling, custom exceptions, and checking for specific error types. The output will be similar to the Go version, showing how different inputs lead to different error handling scenarios.