Skip to main content

Result API

Starting with SDK version 8.0.0, a new generic class, Result<T>, has been introduced. This class is returned by all async API methods. On successful execution, the API returns the subclass Ok<T>, which contains the expected result object (T value). If an error occurs—such as a license issue or an internal exception—the API returns the subclass Error<T>, which contains the error (SBException error), instead of throwing exceptions. Additionally, for RTU UI screens, if the operation is canceled (e.g., when the user dismisses a scanner view), the API returns the subclass Cancel<T>.

The SBException class can be mapped to a specific error type from the list below:

  abstract class SBException implements Exception

class InvalidLicenseException extends SBException
class NullPointerException extends SBException
class InvalidArgumentException extends SBException
class InvalidImageRefException extends SBException
class ComponentUnavailableException extends SBException
class IllegalStateException extends SBException
class IOException extends SBException
class InvalidDataException extends SBException
class OutOfMemoryException extends SBException
class TimeoutException extends SBException
class ProcessException extends SBException
class UnknownException extends SBException

What does this mean in practice? It means you can now handle results in the way that best fits your project. For example:

Handle Result with switch-case (RTU UI)
loading...
Handle Result with if-else statement
loading...

The Result class also provides several helper getter methods that enhance its core functionality:

Handle Result with helper getter methods
handleBarcodeScanningResult(Result<BarcodeScannerUiResult> result) {
// get value or null if unsuccessful
var nullableValue = result.getOrNull();

// get value or throw exception if unsuccessful
try {
var value = result.getOrThrow();
} catch (e) {
// handle error
}
}

Want to scan longer than one minute?

Generate a free trial license to test the Scanbot SDK thoroughly.

Get free trial license