有问题的代码:
final GenericAsyncTask task = new GenericAsyncTask();
task.background = new Runnable() {
public void run() {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(GCMIntentService.this.host);
String addremove = "add";
if(register == false) addremove = "remove";
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
nameValuePairs.add(new BasicNameValuePair("cmd", addremove));
nameValuePairs.add(new BasicNameValuePair("subscription_key", SUBSCRIPTION_KEY)); // unique per app
nameValuePairs.add(new BasicNameValuePair("token", str));
nameValuePairs.add(new BasicNameValuePair("os_family", "android"));
if(addremove.equals("remove")) nameValuePairs.add(new BasicNameValuePair("hard", "" + hard));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(nameValuePairs);
httppost.setEntity(entity);
Log.i(LCHApplication.TAG, "Name Value Pairs: " + nameValuePairs.toString());
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
task.result = EntityUtils.toString(response.getEntity());
} catch (IOException e) {
e.printStackTrace();
}
}
};
task.callback = new Runnable() {
public void run() {
Log.i(LCHApplication.TAG, "registration response: " + task.result.toString());
}
};
task.execute();
大多数情况下效果很好。然而,有时 task.result.toString();
有时是 NULL
,这会引发 java.lang.NullPointerException
。我想保留这次崩溃,因为我希望它在 crashlytics 中报告,这样我就可以看到它影响了多少人。如果我使用 try/catch,我可以抛出什么来确保它不是致命的崩溃?这样它仍会向 crashlytics 报告,但不会终止该应用。
最佳答案
你不必抛出任何捕获的异常,你可以像这样很容易地记录它们:
try {
myMethodThatThrows();
} catch (Exception e) {
Crashlytics.logException(e);
// handle your exception here!
}
来源:http://support.crashlytics.com/knowledgebase/articles/202805-logging-caught-exceptions
关于java - 将致命碰撞变成非致命碰撞分析报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25143120/