Error handling - Android
Introduction
During the execution of Vsdk operations, various exceptions and runtime errors can occur. Proper error handling is essential to ensure application stability, assist in debugging, and recover gracefully from unexpected states.
All exceptions thrown by Vsdk methods are of type com.vivoka.vsdk.Exception, which extends the standard Java Exception class and provides additional diagnostic information specific to the Vsdk.
Handling Exceptions
Any method in the Vsdk may throw a com.vivoka.vsdk.Exception. This exception encapsulates its cause, allowing developers to inspect the full chain of errors for detailed troubleshooting.
Example
import com.vivoka.vsdk.Exception;
try {
Vsdk.init(context, "config/vsdk.json", callback);
}
catch (com.vivoka.vsdk.Exception e) {
// Recommended: print formatted message including cause tree
e.printFormattedMessage();
// Alternatively, retrieve the formatted message as a string
String error = e.getFormattedMessage();
// Because com.vivoka.vsdk.Exception extends java.lang.Exception,
// you can also print the standard stack trace
e.printStackTrace();
}
Notes
printFormattedMessage()displays the full cause tree in a structured format, making it easier to identify the origin of the problem.getFormattedMessage()returns a formatted string representation of the error for logging or display purposes.printStackTrace()may still be used for standard exception tracing.
Resource-Level Errors
When working with Vsdk resources such as recognizers, channels, enhancers, or models, runtime errors may occur during processing. These are reported asynchronously through listener callbacks.
Example
@Override
public void onError(Error<ErrorCode> error) {
// Handle the error
Log.e("VsdkResource", "Error occurred: " + error.toString());
// Release the resource safely
engine.removeRecognizer(/* recognizer reference */);
}
Guidelines
The
onError()callback provides detailed information about the failure, including anErrorCodethat helps identify the issue.When an error occurs, it is recommended to release the affected resource using the appropriate
removemethod on the corresponding engine (e.g.,removeRecognizer(),removeChannel()) and then use it again.Refer to the Managing the SDK and Engine Lifecycle page for details about releasing resources properly.
Best Practices
Always handle
com.vivoka.vsdk.Exceptionexplicitly in your code to prevent application crashes.Use
printFormattedMessage()during development and testing to get comprehensive diagnostic information.Implement robust
onError()handlers for all active resources to ensure that your application can recover gracefully.After an unrecoverable error, consider reinitializing the affected engine or the entire Vsdk if necessary.
Summary
By following these error handling guidelines, you can ensure that your application remains stable, resilient, and easier to debug when working with Vsdk components.