I have a HttpHostConnectException... That is okay, because the server is offline. So I want to mange to catch this exception for the situation, the server will be down.
But if I use
catch (HttpHostConnectException e){
e.printStackTrace();
}
Nothing happens and the exception will be kill the progess. So how can I catch "unreachable" servers? Thank your for your time and help ;)
Calling e.printStackTrace(); will Kill your app as the exception is not handled
e.printStackTrace(); will print the exception on to the logcat and Will show an error or will crash you app
Either you can display the exception as string or Make static text as toast saying server unreachable
catch (HttpHostConnectException e)
{
Toast.makeText(getApplicationContext(), "Server Unreachable ", Toast.LENGTH_LONG).show();
}
if you want to show what was the actual problem / exception that was caused use
catch (HttpHostConnectException e)
{
Toast.makeText(getApplicationContext(), "Connection Timeout Reason "+string.ValueOf(e), Toast.LENGTH_LONG).show();
}
By doing this your app will not kill itself and proceed to the next line of your code
you can also do this
Log.v(locat, exception.toString());
Related
I have the following running at startup inside of an IntentService
threadManager.postBackgroundRunnableDelayed(() -> {
try {
// Calling getToken and getId must be on separate thread. Otherwise, it blocks UI thread.
String token = instanceID.getToken(SENDER_ID, GoogleCloudMessaging.INSTANCE_ID_SCOPE);
String id = instanceID.getId();
Timber.d("id: " + id);
Timber.d("token: " + token);
lazyRestQueue.get().sendGcmToken(new GcmPushRequest(id, token), getSendGcmTokenCallback());
setupAnalytics(token);
} catch (IOException e) {
Timber.e(e, "Exception during registration");
Crashlytics.logException(e);
setHasToken(false);
} catch (RuntimeException e) {
// This will be thrown if Localytics was not properly setup
Timber.e(e, "Exception during registration");
}
});
When I run it on anything below API 18 it causes the app to close (not crash oddly, just close)
can someone give me advice on this? I know it probably has to do with the service or something not timing properly
I have code structurred in this way
public void generalMethod(){
try{
methodThatStartAsyncWebTask();
catch(Exception e){
offlineDataAlternativeMethod();
}
}
the method
public void offlineDataAlternativeMethod(){
try(
loadArchivedFile();
}
catch(Exception e){
reInitializeeData();
}
}
The App crashes at line loadArchivedFile(); that fails because doesn't found the file, but the strange thing is that catch block that invokes reInitializeeData(); isn't reached.
Why cannot reach catch block in anyway? Any idea?
This is the first time that see a similar issue. Any solution?
Try this
getting file not found exception
I just want to ask how do i display the catch exception in android so that i will know if my application catching some error..
example on this.
try {
my codes here.....
} catch (IOException e) {
//how do i dpslay the exception
}
Thank you in advance.
I guess you want some visual (UI) representation of errors.
You can display your errors with Toast, for example.
Or use some library like Crouton. See http://johnkil.github.io/Android-AppMsg/
you can use:
try {
// my codes here.....
} catch(IOException e){
Log.d("MY_APP", "---------------------"); //separator from other logs (optional)
e.printStackTrace();
Log.d("MY_APP", "---------------------"); //separator from other logs (optional)
}
You can use Toast class for instance:
...
catch (IOException e) {
Toast.makeText(<context>, e.getMessage(), Toast.LENGHT_LONG).show();
// or use Log class like Log.e("From class X", e.getMessage());
}
Note if that piece of code is invoked from background Thread you cannot show that message for that that Thread.
In this scenario you need to use another mechanism (runOnUiThread(), Handler, AsyncTask, etc.).
Try doing this...This might help you..
try {
my codes here.....
} catch (IOException e) {
Log.e("e", "exception", e);
}
I get this issue:
"Exception handlers should provide some context and preserve the original exception"
On code like this:
catch (IOException e) {
Log.e(AnkiDroidApp.TAG, "<actual message here");
}
How can I tell to Sonar that our logger isn't Logger, but Log?
Turns out i misunderstood the complaint of Sonar. It was not expecting a specific name for the logger, but for the code to send both the message AND the exception itself to the logger, like so:
catch (IOException e) {
Log.e(AnkiDroidApp.TAG, "<actual message here", e);
}
I'm using Android's httpclient to connect to a domain as follows:
try {
URL url = new URL("example.com");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
read(conn.getInputStream());
} catch (Exception e) {
e.printStackTrace();
}
Now is it fine if I remove this line:
e.printStackTrace();
And replace it with this:
Toast toast = Toast.makeText(getApplicationContext(), "Could not connect to domain.", Toast.LENGTH_LONG);
toast.show();
Or do I have to do something with the 'e' variable? In which case it'll be:
catch (Exception e) {
e.printStackTrace();
Toast toast = Toast.makeText(getApplicationContext(), "Could not connect to domain.", Toast.LENGTH_LONG);
toast.show();
}
And if I do e.printStackTrace(), where does it print to?
I'd recommend catching only the thrown exception type (I think IOException in this case). Other exceptions may be for completely unrelated problems.
Yes, it's fine to replace the printStackTrace with the toast. Bonus tip: Just call
Toast.makeText(getApplicationContext(), "Could not connect to domain.", Toast.LENGTH_LONG).show();
and you can do it all in one line. :)
e.printStackTrace() will print to the standard logcat trace, which you can view with the adb logcat command (adb is part of the Android SDK).