Search code examples
androidexceptionandroid-loadermanagerandroid-cursorloader

Handle CursorLoader exceptions


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.


Solution

  • 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.