4

Some code we have throws different exceptions during it's execution. I want to replace exceptions with Optional; but optional can only inform users that value does not exist. I need more details about various issues that happens during the execution. I need this details mostly for the error messages and getting the right error code.

What is the intended way to use Optional in this case?

igr
  • 10,199
  • 13
  • 65
  • 111

1 Answers1

4

Optional is not intended to replace exceptions, especially if there are different kinds of exceptions and the caller is expected to deal with them.

If your method never returns null unless there is an exception, then introducing Optional to a type that was @NotNull before seems like quite a step backwards regarding type/null-safety.

Just keep the exceptions.

Thilo
  • 257,207
  • 101
  • 511
  • 656