Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8838691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:04:22+00:00 2026-06-14T10:04:22+00:00

I am trying to come up with an exception handling strategy for my Android

  • 0

I am trying to come up with an exception handling strategy for my Android application (although this may also apply to any Java app). For example, I am looking at the delete() ContentProvider function in the example Notepad application:

public int delete(Uri uri, String where, String[] whereArgs) {
    SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    int count;
    switch (sUriMatcher.match(uri)) {
        case NOTES:
            count = db.delete(NOTES_TABLE_NAME, where, whereArgs);
            break;

        case NOTE_ID:
            String noteId = uri.getPathSegments().get(1);
            count = db.delete(NOTES_TABLE_NAME, NoteColumns._ID + "=" + noteId
                    + (!TextUtils.isEmpty(where) ? " AND (" + where + ')' : ""), whereArgs);
            break;

        default:
            throw new IllegalArgumentException("Unknown URI " + uri);
    }

    getContext().getContentResolver().notifyChange(uri, null);
    return count;
}

What happens if the Uri is null? or getContext()? or getContentresolver()?

I have come to the conclusion that the ContentResolver is not the place to catch the exceptions, but it should re-throw them or throw new exceptions so the app can display a meaningful error message.

Would the following or something similar be a bad approach (overkill) – should I just let NullPointerExceptions, etc. bubble up to the top to be handled in a more generic way (as per the example)?.

public int delete(Uri uri, String where, String[] whereArgs) 
    throws SQLiteException, IllegalArgumentException, NotifyException {

    if (null != mOpenHelper) {
       SQLiteDatabase db = mOpenHelper.getWritableDatabase();
       if (null != db) {
           if (null != uri) {
               int count = 0;
               switch (sUriMatcher.match(uri)) {
                   case NOTES:
                       count = db.delete(NOTES_TABLE_NAME, where, whereArgs);
                       break;

                   case NOTE_ID:
                       String noteId = uri.getPathSegments().get(1);
                       count = db.delete(NOTES_TABLE_NAME, NoteColumns._ID + "=" + noteId
                        + (!TextUtils.isEmpty(where) ? " AND (" + where + ')' : ""), whereArgs);
                       break;

                   default:
                       throw new IllegalArgumentException("Unknown URI " + uri);
               }

               if (null != getContext()) && (null != getContentResolver()) {
                   getContext().getContentResolver().notifyChange(uri, null);
               } else {
                   throw NotifyException("Failed to notify change");
               }
               return count;
            } else {
                throw new IllegalArgumentException("Must provide URI");
            }
        } else {
            throw new SQLiteException("Failed to get database");
        }
    } else {
        throw new SQLiteException("Invalid database helper");
    }
}

Disclaimer: this code may not compile! it is an example.

It certainly is harder to read! I don’t know what the right balance is and need some help!.

Update: I read through Android’s recommended practices (see http://source.android.com/source/code-style.html#java-language-rules) but it just confused me further!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T10:04:25+00:00Added an answer on June 14, 2026 at 10:04 am

    The idea of handling exception is to show a clear reason why an error is happened and maybe to perform some actions depending on the error. So, please consider:

    1. Define your own exception class derived from Java exception

    2. Put your code in try/catch blocks. When you catch an exception you log it with a trace and then throw your own exception with clear explanation.

    3. At some point you catch your own exception, extract information and
      show the information either on a screen or send back to a program user if you provide a Web Service

    Code Example:

    public class MyException extends Exception {
    
    public static final String MYSeparator = "!@#!";
    public MyException() {
        super();
    }
    
    public MyException(String message) {
        super(message);
    }
    
    public MyException(Throwable cause) {
        super(cause);
    }
    
    public MyException(String message, Throwable cause) {
        super(message, cause);
    }
    
    public MyException(String errorCode, String errorDescription,
            Throwable cause) {
        super(errorCode + MYSeparator + errorDescription, cause);
    }
    
    public MyException(String errorCode, String errorDescription) {
        super(errorCode + MYSeparator + errorDescription);
    }
    

    }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to come up with a function that lets me pass in any
How come I get an invalid cast exception when trying to set a NULL
I am trying to set up my app to throw a timeout exception after
im trying to do something similar to this: android: how to listen to "sd
We are builing a windows desktop application (not web based) and trying to come
I'm trying to recursively get a directory structure in Java, on an Android emulator,
I've been trying to diagnose a memory leak in an Android application I'm writing.
I am trying to come up with a manageable way to handle exceptions in
im trying to come up with a design for a wrapper for use when
I'm trying to come up with an algorithm that will do the following: If

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.