是否可以捕获java.lang.reflect.GenericSignatureFormatError?

时间:2019-06-08 09:33:26

标签: java android gson

在尝试捕获块中调用了

com.google.gson.Gson.fromJson(),但尽管很少

java.lang.reflect.GenericSignatureFormatError: 
  at libcore.reflect.GenericSignatureParser.parseFieldTypeSignature (GenericSignatureParser.java:297)
  at libcore.reflect.GenericSignatureParser.parseTypeArgument (GenericSignatureParser.java:374)
  at libcore.reflect.GenericSignatureParser.parseOptTypeArguments (GenericSignatureParser.java:344)
  at libcore.reflect.GenericSignatureParser.parseClassTypeSignature (GenericSignatureParser.java:317)
  at libcore.reflect.GenericSignatureParser.parseFieldTypeSignature (GenericSignatureParser.java:289)
  at libcore.reflect.GenericSignatureParser.parseForField (GenericSignatureParser.java:218)
  at java.lang.reflect.Field.getGenericType (Field.java:177)
  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields (ReflectiveTypeAdapterFactory.java:160)
  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create (ReflectiveTypeAdapterFactory.java:102)
  at com.google.gson.Gson.getAdapter (Gson.java:457)
  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField (ReflectiveTypeAdapterFactory.java:117)
  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields (ReflectiveTypeAdapterFactory.java:166)
  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create (ReflectiveTypeAdapterFactory.java:102)
  at com.google.gson.Gson.getAdapter (Gson.java:457)
  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField (ReflectiveTypeAdapterFactory.java:117)
  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields (ReflectiveTypeAdapterFactory.java:166)
  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create (ReflectiveTypeAdapterFactory.java:102)
  at com.google.gson.Gson.getAdapter (Gson.java:457)
  at com.google.gson.Gson.fromJson (Gson.java:921)
  at com.google.gson.Gson.fromJson (Gson.java:860)

有没有办法捕获java.lang.reflect.GenericSignatureFormatError来防止应用程序崩溃?

编辑:代码如下:

try{
    ...
    foo = _gson.fromJson(bufferedReader, Foo.class); 
    ...
}catch(Exception ex) {
    ...
}

也许我以catch(Exception ex)捕获所有东西为前提的假设是错误的。

2 个答案:

答案 0 :(得分:2)

Hong,您可以按照以下方式处理。

try{
    ...
    foo = _gson.fromJson(bufferedReader, Foo.class); 
    ...
}catch(Exception ex) {
    ...
}
catch(Error err) {
    ...
}

永远记住,任何类型为Error的错误,例如NoClassDefFoundErrorNoSuchMethodError都不会陷入catch(Exception ex)块中。但是,错误是无法恢复的。您可以在Android应用中显示一些弹出消息。

答案 1 :(得分:1)

尝试使用此-

try{

            //do your work here
        }catch (GenericSignatureFormatError e){
            e.printStackTrace();
        }

避免应用崩溃。