-
Notifications
You must be signed in to change notification settings - Fork 2
ExceptionHandler
When it comes to exceptions in an Android application, it may become very tedious and cumbersome to systematically handle them properly. Very often, many developers end up in "swallowing" those exception silently, which is obviously a very bad thing, because it takes too much time to investigate for its cause and display the right user interface (Toast, Dialog...). However, the quality of a software significantly depends on the way exceptions are handled: good programmers know that, and they also know that handling properly exceptions takes time and prompts them to define consistent policies.
The typical case are exceptions raised due to bad network connectivity: every time a web service service is invoked, that kind of exception may be thrown, and if you want to provide a descent end-user experience, you ought to handle them properly, and not just ignoring them.
Since Android does not offer that kind of feature natively, that's where the "ExceptionHandler" comes in hand: it has been designed for enabling to centralize in a single place all exceptions thrown by an Android application. Whatever the piece of code which causes the exception to be triggered, the ExceptionHandler will be invoked for letting you handle the exception. The main places where exceptions are thrown is while retrieving business objects, and running some code in a detached thread.
In order to benefit from the "ExceptionHandler" component, you need to take the following steps:
- define a class which implements the
ExceptionHandler
interface, or use one of the already existing like theAbstractExceptionHandler
; - declare this class in the
ActivityController
though theregisterExceptionHandler()
method. The typical location where to register that exception handler is when the Android application starts, in theApplication.onCreate()
method. In the case of the framework, if you use theSmartApplication
, this will be automatically done for you through theSmartApplication.getExceptionHandler()
method ; - derive your Android activities from one of the
Smartable
classes provided by the framework ; - whenever you perform something in a background/detached thread, use the
SmartCommands.execute()
method.
If you have registered an ExceptionHandler, it will be involved by the framework in two Exception
cases, which we refer to as "managed exceptions":
- when retrieving business objects in your Android
Smartable
activities through theLifeCycle.onRetrieveBusinessObjects
: when something goes wrong, throw aLifeCycle.BusinessObjectUnavailableException
exception ; - when using the
SmartCommand.execute()
method and an exception is thrown during the command execution.
If you derive your Android Application
from the SmartApplication, you just need to implement the SmartApplication.getExceptionHandler()
method and return your own implementation. The two previously mentioned cases will be automatically covered by the ExceptionHandler
.
This component exposes 4 methods, for which you will find documentation in the JavaDoc. In particular, two methods will be invoked when exceptions are raised:
-
onBusinessObjectAvailableException()
: if aBusinessObjectUnavailableException
exception is thrown during the retrieval of business objects during theLifeCycle.onRetrieveBusinessObjects()
method ; -
onOtherException()
: when running a command through theSmartCommands.execute()
method, if the command execution throws an exception. If you decide to use theSimpleGuardedCommmand
as a command when running such a command, you will receive aGuardedException
exception if the command execution throws an exception, which lets you, for instance, display a dialog box or a toast for notifying the end-user.
This handler is a good place for handling those exceptions: since the methods above provide the activity which originated the exception, and well as the exception itself, you can handle in a centralized way certain kinds of exceptions.
This class is a first implementation of ExceptionHandler
which can be used out-of-the-box. You may derive to use it as is, or to derive from it. For every handled exception, it checks if it is due to an Internet connectivity problem through its checkConnectivityProblemInCause()
method. Please, take a look at the source code implementation, in order to tailor yours.
With the ExceptionHandler component, you can now centralize the way to handle all your Android application exceptions in one place. If you have integrated an error reporting system in your application, it is also a good place for logging to that system.