writing an exception result at the header of a parcel - Android Android OS

Android examples for Android OS:Parcel

Description

writing an exception result at the header of a parcel

Demo Code

/*//from   ww  w .jav a2s.c  om
 * Copyright (C) 2006 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
//package com.book2s;

import java.io.FileNotFoundException;

import android.content.OperationApplicationException;
import android.database.sqlite.SQLiteAbortException;
import android.database.sqlite.SQLiteConstraintException;
import android.database.sqlite.SQLiteDatabaseCorruptException;
import android.database.sqlite.SQLiteDiskIOException;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteFullException;
import android.os.OperationCanceledException;
import android.os.Parcel;
import android.util.Log;

public class Main {
  private static final String TAG = "DatabaseUtils";

  /**
   * Special function for writing an exception result at the header of a parcel,
   * to be used when returning an exception from a transaction. exception will
   * be re-thrown by the function in another process
   * 
   * @param reply
   *          Parcel to write to
   * @param e
   *          The Exception to be written.
   * @see Parcel#writeNoException
   * @see Parcel#writeException
   */
  public static final void writeExceptionToParcel(Parcel reply, Exception e) {
    int code = 0;
    boolean logException = true;
    if (e instanceof FileNotFoundException) {
      code = 1;
      logException = false;
    } else if (e instanceof IllegalArgumentException) {
      code = 2;
    } else if (e instanceof UnsupportedOperationException) {
      code = 3;
    } else if (e instanceof SQLiteAbortException) {
      code = 4;
    } else if (e instanceof SQLiteConstraintException) {
      code = 5;
    } else if (e instanceof SQLiteDatabaseCorruptException) {
      code = 6;
    } else if (e instanceof SQLiteFullException) {
      code = 7;
    } else if (e instanceof SQLiteDiskIOException) {
      code = 8;
    } else if (e instanceof SQLiteException) {
      code = 9;
    } else if (e instanceof OperationApplicationException) {
      code = 10;
    } else if (e instanceof OperationCanceledException) {
      code = 11;
      logException = false;
    } else {
      reply.writeException(e);
      Log.e(TAG, "Writing exception to parcel", e);
      return;
    }
    reply.writeInt(code);
    reply.writeString(e.getMessage());

    if (logException) {
      Log.e(TAG, "Writing exception to parcel", e);
    }
  }
}

Related Tutorials