Errors in Fortress

In Java, error handling is typically done through exceptions, which is different from the explicit error return values used in some other languages. Java uses a try-catch mechanism to handle exceptions. Let’s see how we can implement similar functionality:

import java.util.Arrays;

public class ErrorHandling {

    // Custom exception classes
    public static class CustomException extends Exception {
        public CustomException(String message) {
            super(message);
        }
    }

    public static class OutOfTeaException extends Exception {
        public OutOfTeaException() {
            super("no more tea available");
        }
    }

    public static class PowerException extends Exception {
        public PowerException() {
            super("can't boil water");
        }
    }

    // Method that may throw an exception
    public static int f(int arg) throws CustomException {
        if (arg == 42) {
            throw new CustomException("can't work with 42");
        }
        return arg + 3;
    }

    // Method that may throw different types of exceptions
    public static void makeTea(int arg) throws OutOfTeaException, PowerException {
        if (arg == 2) {
            throw new OutOfTeaException();
        } else if (arg == 4) {
            throw new PowerException();
        }
    }

    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 (OutOfTeaException e) {
                System.out.println("We should buy new tea!");
            } catch (PowerException e) {
                System.out.println("Now it is dark.");
            } catch (Exception e) {
                System.out.println("unknown error: " + e.getMessage());
            }
        }
    }
}

In Java, we use exceptions to handle errors. Here’s how the concepts translate:

  1. Instead of returning an error, we throw an exception.
  2. Custom errors are implemented as custom exception classes.
  3. The try-catch block is used to handle exceptions, similar to checking for non-nil errors.
  4. We can catch specific types of exceptions, which is similar to using errors.Is() in some other languages.

Java’s exception handling mechanism allows for a hierarchy of exceptions, which can be useful for grouping related error types. The throws clause in method signatures explicitly declares which exceptions a method might throw, providing information to the caller about potential error conditions.

When you run this program, you’ll see output similar to:

f worked: 10
f failed: can't work with 42
Tea is ready!
Tea is ready!
We should buy new tea!
Tea is ready!
Now it is dark.

This example demonstrates how to create and use custom exceptions, throw exceptions in methods, and catch and handle different types of exceptions in Java.