Class ProblemException
- All Implemented Interfaces:
Serializable
Thrown from ResultOrProblems.getOrThrow()
as an alternative to using functional style flow control in your
code with ResultOrProblems
. ProblemException
is a checked exception and is explicitly meant to be
- it is ised for exceptional flow control: calling code must decide how to deal with the exception;
declaring code should be careful that they should be using this instead of returning a ResultOrProblems.
Use with catching(Call)
to 'clean' up exceptional flow control within a method body.
This is in contrast to a ResultComputationException
which is a programming error caused by not checking for a
result before calling get()
. ResultComputationException is an unchecked exception and is not meant to be caught
(apart from error reporting code).
Example use:
```
public ResultOrProblems
return new Foo(listType, expr);
});
}
``
A small number of micro benchmarks were done to compare the relative costs of throwing/catching a ProblemException,
vs returning a ResultOrProblems, including the cost of using the
wrap` method to clean up code. Findings were:
- throwing exceptions is slower than returning a failed ResultOrProblems, if suppressing stack traces then it becomes almost neglible (say, 20% slower)
- throwing exceptions is faster when code mostly succeeds - avoids the overhead of constructing a wrapper object for each method call
- using the
catching(Call)
method adds overhead, say, a 10x overhead to succeeding code and a 2x overhead to failing code. But this was 10ms vs 100ms for 100k iterations around a trivial method.
Ultimately this shows that for code that's not in a tight loop or being called a lot, worry about write nice looking code, don't worry about performance.
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
A function call that will either return an object or throw aProblemException
.static interface
Alternative toProblemException.Call
that accepts a list of problems to return in the result -
Constructor Summary
ConstructorDescriptionProblemException
(List<Problem> problems) ProblemException
(Problem... problems) Create a newProblemException
-
Method Summary
Modifier and TypeMethodDescriptionstatic final <T> ResultOrProblems<T>
catching
(ProblemException.Call<T> call) static final <T> ResultOrProblems<T>
catching
(ProblemException.ProblemsCall<T> call) static void
throwUnlessEmpty
(List<Problem> problems) <T> ResultOrProblems<T>
toResult()
Methods inherited from class java.lang.Throwable
addSuppressed, getCause, getLocalizedMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString
-
Constructor Details
-
ProblemException
Create a new
ProblemException
-
ProblemException
-
-
Method Details
-
throwUnlessEmpty
- Throws:
ProblemException
-
catching
Wrap some code in a try-catch so that the result of
T
is wrapped in aResultOrProblems
of typeT
. If the code throws aProblemException
, a failed result is returned.- Type Parameters:
T
- type the return type of the call- Parameters:
call
- the code to call- Returns:
- Either a successful result of type T or a failed result.
-
catching
Wrap some code in a try-catch so that the result of
T
is wrapped in aResultOrProblems
of typeT
. If the code throws aProblemException
, a failed result is returned. Any problems added in to the list will also be returned. If the problems list has errors, then a failed result will be returned (ignoring any result that might have been returned).- Type Parameters:
T
- type the return type of the call- Parameters:
call
- the code to call- Returns:
- Either a successful result of type T or a failed result.
-
toResult
- Returns:
- a failed ResultOrProblems that includes the problems attached to this exception
-
fillInStackTrace
- Overrides:
fillInStackTrace
in classThrowable
-
getProblems
-
getMessage
- Overrides:
getMessage
in classThrowable
-