check Exception Type - Android java.lang

Android examples for java.lang:Throwable

Description

check Exception Type

Demo Code


import android.content.Context;
import android.content.SharedPreferences;
import android.widget.Toast;

public class Main{
    public static final String TAG = "CommonUtil";
    /**//  www  .  j  a  v  a 2s .c o  m
     * 
     * @param tag
     *            Log tag
     * @param e
     *            Exception
     * @return 1: org.apache.http.conn.ConnectTimeoutException<br>
     *         2: org.apache.http.conn.HttpHostConnectException<br>
     *         3: org.json.JSONException<br>
     *         4: java.net.SocketTimeoutException<br>
     *         5: java.net.ConnectException<br>
     *         6: java.io.FileNotFoundException<br>
     *         7: java.net.UnknownHostException<br>
     *         8: java.net.SocketException<br>
     * <br>
     */
    public static int checkExceptionType(String tag, Exception e) {
        int result = -1;

        if (e instanceof org.apache.http.conn.ConnectTimeoutException) {
            Logger.e(TAG,
                    "-->Exception Type : org.apache.http.conn.ConnectTimeoutException");
            result = 1;
        } else if (e instanceof org.apache.http.conn.HttpHostConnectException) {
            Logger.e(TAG,
                    "-->Exception Type : org.apache.http.conn.HttpHostConnectException");
            result = 2;
        } else if (e instanceof org.json.JSONException) {
            Logger.e(TAG, "-->Exception Type : org.json.JSONException");
            result = 3;
        } else if (e instanceof java.net.SocketTimeoutException) {
            Logger.e(TAG,
                    "-->Exception Type : java.net.SocketTimeoutException");
            result = 4;
        } else if (e instanceof java.net.ConnectException) {
            Logger.e(TAG, "-->Exception Type : java.net.ConnectException");
            result = 5;
        } else if (e instanceof java.io.FileNotFoundException) {
            Logger.e(TAG,
                    "-->Exception Type : java.io.FileNotFoundException");
            result = 6;
        } else if (e instanceof java.net.UnknownHostException) {
            Logger.e(TAG,
                    "-->Exception Type : java.net.UnknownHostException");
            result = 7;
        } else if (e instanceof java.net.SocketException) {
            Logger.e(TAG, "-->Exception Type : java.net.SocketException");
            result = 8;
        }
        return result;
    }
}

Related Tutorials