title: AsyncTask and error handling on Android
body: <p>I'm converting my code from using <code>Handler</code> to <code>AsyncTask</code>. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread.  What's unclear to me is how to handle exceptions if something goes haywire in <code>AsyncTask#doInBackground</code>. </p>
  
  <p>The way I do it is to have an error Handler and send messages to it. It works fine, but is it the "right" approach or is there better alternative?</p>
  
  <p>Also I understand that if I define the error Handler as an Activity field, it should execute in the UI thread.  However, sometimes (very unpredictably) I will get an Exception saying that  code triggered from <code>Handler#handleMessage</code> is executing on the wrong thread. Should I initialize error Handler in <code>Activity#onCreate</code> instead? Placing <code>runOnUiThread</code> into <code>Handler#handleMessage</code> seems redundant but it executes very reliably.</p>