6

I have a Fragment implementing LoaderManager and using CursorLoader (nothing fancy). I want to catch exceptions thrown during the query but I don't see how!!! Any help? Thx.

David Snabel-Caunt
  • 57,804
  • 13
  • 114
  • 132
denispyr
  • 1,403
  • 3
  • 21
  • 34
  • Your question is a bit unclear. What kind of exceptions you need to catch? Have you tried try & catch blocks? – Mohamed_AbdAllah Nov 25 '12 at 14:05
  • I have something like this: – denispyr Nov 25 '12 at 14:57
  • My Fragment implements LoaderManager and implements onCreateLoader, onLoadFinished and onLoaderReset. The onCreateLoader method creates a CursorLoader instance. So far so good, nothing fancy. Now, say that the cursor throws an exception when it tries to run and deliver content to the Fragment. Where I can catch it? Mind that my code never explicitly run the cursor, this is done by the loader. – denispyr Nov 25 '12 at 15:04
  • I want to do something similar to [AsyncTask and error handling on Android](http://stackoverflow.com/questions/1739515/asynctask-and-error-handling-on-android/1739676#1739676) – denispyr Nov 25 '12 at 17:53
  • So *all* queries run normally *all* the time!?!? I am the only one that gets query execution exception *and* want to handle them?!?! (Am I doing something totally wrong?) – denispyr Nov 26 '12 at 14:52
  • +1 for your question. I'm using LoaderEx SQLiteCursorLoader and I'll try to handle it in the source... Its unacceptable that a query can bring the whole app down. – Rafael Nobre Dec 05 '12 at 16:48

2 Answers2

3

I tried to inherit and implement a listener, then I tried to inherit and implement a callback. The most simple and less intrusive solution, in my case, seems to be the following

public class CursorLoaderGraceful extends CursorLoader {
    public Throwable error; // holder
    public CursorLoaderGraceful(Context context) {
        super(context);
    }
    public CursorLoaderGraceful(Context context, Uri uri, String[] projection, String selection,
            String[] selectionArgs, String sortOrder) {
        super(context, uri, projection, selection, selectionArgs, sortOrder);
    }
    public void OnQueryException(RuntimeException throwable) {
        throw throwable;
    }

    @Override
    public Cursor loadInBackground() {
        try {
            return (super.loadInBackground());
        } catch (Throwable t) {
            error = t; // keep it
        }
        return (null);
    }
}

And in the fragment / activity

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    CursorLoaderGraceful loader = new CursorLoaderGraceful(this., other, params, go , here);
    // ...
    return loader;
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    //trivial code
    mAdapter.swapCursor(data);
    if (this.isResumed()) {
        this.setListShown(true);
    } else {
        this.setListShownNoAnimation(true);
    }

    //check and use
    Throwable loaderError = ((CursorLoaderGraceful)loader).error;
    if (loaderError != null) {
        //all these just to show it?!?!? :/
        Toast.makeText(this, loaderError.getMessage(), Toast.LENGTH_SHORT)
                .show();
    }
}
denispyr
  • 1,403
  • 3
  • 21
  • 34
1

You would need to derive from CursorLoader to do it. Something like this:

class MyCursorLoader extends CursorLoader {

    public MyCursorLoader(Context context) {
         super(context)
      }

    public CursorLoader(Context context, Uri uri, String[] projection, String selection,
            String[] selectionArgs, String sortOrder) {
        super(context, uri, projection, selection, selectionArgs, sortOrder);
    }

    @Override
    public Cursor loadInBackground() {

        try {
            return (super.loadInBackground);
        } catch (YourException e) {
            // Do your thing.
        }

        return (null);
    }

}

You can adapt it to implement your error handling.

denispyr
  • 1,403
  • 3
  • 21
  • 34
jsmith
  • 4,847
  • 2
  • 32
  • 39