Design Pattern for Finding Unhandled Exceptions in AsyncTask

People,

I will catch unhandled exceptions for Android with a piece of code like this at the top of onCreate:

try { File crashLogDirectory = new File(Environment.getExternalStorageDirectory().getCanonicalPath() + Constants.CrashLogDirectory); crashLogDirectory.mkdirs(); Thread.setDefaultUncaughtExceptionHandler(new RemoteUploadExceptionHandler( this, crashLogDirectory.getCanonicalPath())); } catch (Exception e) { if (MyActivity.WARN) Log.e(ScruffActivity.TAG, "Exception setting up exception handler! " + e.toString()); } 

I would like to come up with something similar for about two dozen AsyncTasks that I use in my Android application, so the unhandled exceptions that occur in doInBackground get caught and logged.

The problem is that AsyncTask accepts any type of initializers, I'm not sure how to declare a superclass from which all my AsyncTasks inherits, which sets this unhandled exception handler.

Can anyone recommend a good design pattern for handling unhandled exceptions in the doInBackground AsyncTask method, which does not require copying and pasting the code like above for each new AsyncTask definition?

Thanks!

UPDATE

Here is the design pattern I used after a closer look at the AsyncTask source

 import java.io.File; import android.content.Context; import android.os.AsyncTask; import android.os.Environment; import android.util.Log; public abstract class LoggingAsyncTask<Params, Progress, Result> extends AsyncTask<Params, Progress, Result> { protected void setupUnhandledExceptionLogging(Context context) { try { File crashLogDirectory = new File(Environment.getExternalStorageDirectory().getCanonicalPath() + Constants.CrashLogDirectory); crashLogDirectory.mkdirs(); Thread.setDefaultUncaughtExceptionHandler(new RemoteUploadExceptionHandler( context, crashLogDirectory.getCanonicalPath())); } catch (Exception e) { if (MyActivity.WARN) Log.e(ScruffActivity.TAG, "Exception setting up exception handler! " + e.toString()); } } } 

Then I define my tasks as follows:

 private class MyTask extends LoggingAsyncTask<Void, Void, HashMap<String, Object>> { protected HashMap<String, Object> doInBackground(Void... args) { this.setupUnhandledExceptionLogging(MyActivity.this.mContext); // do work return myHashMap; } } 

Obviously, your task can accept any parameters necessary for this template. For you, you need to define RemoteUploadExceptionHandler to perform the necessary registration / loading.

+8
java android exception android-asynctask unhandled-exception
source share
2 answers

I would not call it a design pattern like that, but simply wrapped doInBackground() and initialized and / or caught exceptions as needed.

 public abstract class AsyncTaskWrapper<Params, Progress, Result> extends AsyncTask<Params, Progress, Result> { protected Exception error; protected Result doInBackground(Params... params) { try { init(); return doRealWork(params); } catch (Exception e) { error = e; Log.e("TAG", e.getMessage(), e); return null; } } protected abstract void init(); protected abstract Result doRealWork(Params... params); } 
+3
source share

esilver I am an exception trap and returns a BoolString object, a template without throws

  //a utility class to signal success or failure, return an error message, and return a useful String value //see Try Out in C# public final class BoolString { public final boolean success; public final String err; public final String value; public BoolString(boolean success, String err, String value){ this.success= success; this.err= err; this.value= value; } } 

APPLICATION:

 private class MyAsynch extends AsyncTask<String, Void, BoolString>{ protected BoolString doInBackground(String...strings) { // <== DO NOT TOUCH THE UI VIEW HERE return model.tryMyMethod(...); // <== return value BoolString result is sent to onPostExecute } protected void onPostExecute(BoolString result){ progress.dismiss(); if (result.success){ ... continue with result.value } else { ..log result.err } } // NO THROWS VERSION Helper method public BoolString tryMyMethod(...) { try { String value= MyMethod(...); return new BoolString(true,"",value); } catch (Exception e){ return new BoolString(false,e.getMessage(),""); } } 
0
source share

All Articles