【问题标题】:Can't catch Java (Android) Exception with try-catch无法使用 try-catch 捕获 Java (Android) 异常
【发布时间】:2013-09-30 18:40:12
【问题描述】:

我是一名 Java (Android) 初学者(来自 Python),我正在尝试使用 Try-Catch 捕获异常,如下所示:

try {
    u.save();
} catch (Exception e) {
    Log.wtf("DO THIS", " WHEN SAVE() FAILS");
}

令我惊讶的是,我没有看到我的日志消息,但我仍然收到以下错误:

09-25 10:53:32.147: E/SQLiteDatabase(7991): android.database.sqlite.SQLiteConstraintException:错误代码 19: 约束失败

为什么它没有捕捉到异常?我在这里做错了吗?欢迎所有提示!

save() 方法如下所示:

public final void save() {
    final SQLiteDatabase db = Cache.openDatabase();
    final ContentValues values = new ContentValues();

    for (Field field : mTableInfo.getFields()) {
        final String fieldName = mTableInfo.getColumnName(field);
        Class<?> fieldType = field.getType();

        field.setAccessible(true);

        try {
            Object value = field.get(this);

            if (value != null) {
                final TypeSerializer typeSerializer = Cache.getParserForType(fieldType);
                if (typeSerializer != null) {
                    // serialize data
                    value = typeSerializer.serialize(value);
                    // set new object type
                    if (value != null) {
                        fieldType = value.getClass();
                        // check that the serializer returned what it promised
                        if (!fieldType.equals(typeSerializer.getSerializedType())) {
                            Log.w(String.format("TypeSerializer returned wrong type: expected a %s but got a %s",
                                    typeSerializer.getSerializedType(), fieldType));
                        }
                    }
                }
            }

            // TODO: Find a smarter way to do this? This if block is necessary because we
            // can't know the type until runtime.
            if (value == null) {
                values.putNull(fieldName);
            }
            else if (fieldType.equals(Byte.class) || fieldType.equals(byte.class)) {
                values.put(fieldName, (Byte) value);
            }
            else if (fieldType.equals(Short.class) || fieldType.equals(short.class)) {
                values.put(fieldName, (Short) value);
            }
            else if (fieldType.equals(Integer.class) || fieldType.equals(int.class)) {
                values.put(fieldName, (Integer) value);
            }
            else if (fieldType.equals(Long.class) || fieldType.equals(long.class)) {
                values.put(fieldName, (Long) value);
            }
            else if (fieldType.equals(Float.class) || fieldType.equals(float.class)) {
                values.put(fieldName, (Float) value);
            }
            else if (fieldType.equals(Double.class) || fieldType.equals(double.class)) {
                values.put(fieldName, (Double) value);
            }
            else if (fieldType.equals(Boolean.class) || fieldType.equals(boolean.class)) {
                values.put(fieldName, (Boolean) value);
            }
            else if (fieldType.equals(Character.class) || fieldType.equals(char.class)) {
                values.put(fieldName, value.toString());
            }
            else if (fieldType.equals(String.class)) {
                values.put(fieldName, value.toString());
            }
            else if (fieldType.equals(Byte[].class) || fieldType.equals(byte[].class)) {
                values.put(fieldName, (byte[]) value);
            }
            else if (ReflectionUtils.isModel(fieldType)) {
                values.put(fieldName, ((Model) value).getId());
            }
            else if (ReflectionUtils.isSubclassOf(fieldType, Enum.class)) {
                values.put(fieldName, ((Enum<?>) value).name());
            }
        }
        catch (IllegalArgumentException e) {
            Log.e(e.getClass().getName(), e);
        }
        catch (IllegalAccessException e) {
            Log.e(e.getClass().getName(), e);
        }
    }

    if (mId == null) {
        mId = db.insert(mTableInfo.getTableName(), null, values);
    }
    else {
        db.update(mTableInfo.getTableName(), values, "Id=" + mId, null);
    }

    Cache.getContext().getContentResolver()
            .notifyChange(ContentProvider.createUri(mTableInfo.getType(), mId), null);
}

【问题讨论】:

  • 您是否也看到了您的日志消息?
  • @Henry - 不,我根本看不到错误消息。 (将其添加到我的问题中)。
  • 尝试用“e”替换“wtf” - 然后您应该会看到您的消息。您的 try/catch 没有任何问题。除非 u.save 方法已经捕获到异常并处理它
  • 你能告诉我们save方法吗?
  • 可能是您的 save 方法中捕获了异常

标签: java android sqlite exception try-catch


【解决方案1】:

有两个类可以解决问题。

  1. 错误
  2. 异常

两者都是 Throwable 类的子类。当出现我们不知道的情况时,那个特定的代码块会抛出异常还是错误?您可以使用Throwable。 Throwable 将捕获 错误 和 异常。

这样做

try {
    u.save();
} catch (Throwable e) {
    e.printStackTrace();
}

【讨论】:

  • 这给出了奇怪的结果,它没有显示 SQLException,但它也没有显示日志消息.. :S
  • 这很有意义 + 这只是节省了我的一天谢谢!
  • 提示:如果您的代码中出现“java.lang.NullPointerException”,请使用上面的“printStackTrace()”方法,就像示例一样。它会为你的问题带来光明。
  • 当我尝试获取微调器值时,它仍然无法帮助我的应用程序崩溃
【解决方案2】:

这样做

try {
    // do some thing which you want in try block
} catch (JSONException e) {
    e.printStackTrace();
    Log.e("Catch block", Log.getStackTraceString(e));
} 

【讨论】:

    【解决方案3】:

    试试

    try {
    u.save();
    
    } catch (SQLException sqle) {
    Log.wtf("DO THIS", " WHEN SAVE() FAILS");
    }catch (Exception e) {
    Log.wtf("DO THIS", " WHEN SAVE() FAILS");
    }
    

    【讨论】:

      【解决方案4】:

      约束失败通常表示您在创建表时将null 值传递到您声明为不是null 的列中。

      【讨论】:

      • 完全正确。我是故意这样做的,因为我想在它发生时纠正这个错误..
      • 这个答案确实很有用,但它并没有解决我的问题。如果你看到 save() 方法,你知道如何捕捉错误吗?
      【解决方案5】:

      Log 需要某些变量名,例如 verbose(v)、debug(d) 或 info(i)。 你的“wtf”不属于那里。检查此答案以获取更多信息-->

      https://stackoverflow.com/a/10006054/2074990

      或者这个:

      http://developer.android.com/tools/debugging/debugging-log.html

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-08
      • 1970-01-01
      • 2017-08-07
      • 2014-03-22
      • 1970-01-01
      • 2015-06-21
      相关资源
      最近更新 更多