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 9092581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:52:35+00:00 2026-06-16T22:52:35+00:00

I have an AsyncTask to do SQLite database migration in the background (create or

  • 0

I have an AsyncTask to do SQLite database migration in the background (create or upgrade). Let’s say somehow an IOException or SQLiteException is thrown inside doInBackground and it’s pointless for the app to continue running because database state might be not in desired state. I’m kind of confused on what to do in this situation.

I’m thinking about letting the application crash as soon as possible and show dialog with error message, but I’m not really sure how to this inside doInBackground, because:

  1. This function is not executed in UI thread so I don’t know if I can show a dialog.
  2. I don’t know how to access current activity within AsyncTask, so I can’t finish() it.
  3. I want to somehow throw the exception to the upper layer and let an activity handle it, but it’s not possible because doInBackground doesn’t list IOException as a checked exception.

Anyone has an advice on how to gracefully handle this situation?

  • 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-16T22:52:36+00:00Added an answer on June 16, 2026 at 10:52 pm

    You can’t show dialog in non-ui thread. You can pass activity reference to async task. To handle this situation you can try catch the exception in doInBackground and re-throw it in onPostExecute

    e.g.

    private class MyAsyncTaskTask extends AsyncTask<...> {
    
         private Activity ownerActivity;
         private Exception exceptionToBeThrown;
    
         public MyAsyncTaskTask(Activity activity) {
             // keep activity reference
             this.ownerActivity = activity;
         }
    
         protected Long doInBackground(...) {
             try {
                 ...
             } catch (Exception e) {
                 // save exception and re-thrown it then. 
                 exceptionToBeThrown = e;
             }
         }
    
         protected void onPostExecute(...) {
             // Check if exception exists.
             if (exceptionToBeThrown != null) {
                 ownerActivity.handleXXX();
                 throw exceptionToBeThrown;
             }
         }
     }
    

    If you async task is in Acvitiy class, then you can directly access it, e.g.,

    public class MyActivity extends Activity {
        ...
        AsyncTask<...> task = new AsyncTask<...>() {
            public void onPostExecute(...) {
                // Access activity directly
                MyActivity.this.xxx()
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an AsyncTask and on the background I want to start Service that
I have the following asynctask class which is not inside the activity. In the
I have two total different implementations of AsyncTask , lets say AT1 and AT2
I have AsyncTask doing background work and populating ListView in its onProgressUpdate and calling
I have an AsyncTask class inside my MapActivity. Inside the doInBacground() method I have
whenever I use the database I create a new instance of SQLOpenHelper. I have
I have AsyncTask that processes some background HTTP stuff. AsyncTask runs on schedule (Alarms/service)
I have done some measurements regarding the IO on my SQLite database. It takes
I have an AsyncTask inside an Service which is trying to pass a ArrayList
I have AsyncTask and my progress in background is in infinite loop. But I

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.