How to handle exception and prevent crashing app - android

I am using the Amazon AWS SDK to download images from S3. Occasionally, when an image is not found an exception "AmazonS3Exception: Status Code: 404" is thrown. However, this seems like an exception which should not crash the app. How can I handle this exception so that it does not crash the app? Apologies, Im a noob to Java & Android.

To follow up on type-a1pha's answer:
If you want to handle an exception gracefully, you would use a try-catch statement. It works something like this:
try {
// Here you put the code that may throw an exception
} catch (AmazonS3Exception e) {
// Looks like we errored out, log the exception and
// tell the user that we 404'd
Log.e(TAG, "Error fetching file from Amazon S3", e);
Toast.makeText(context, "Error 404 while fetching file: file not found", Toast.LENGTH_SHORT).show();
// Insert any other code you need here to recover from the error
} finally {
// Note that the finally part is optional but useful if you want
// to do something after the try-catch statement is finished
// for example, if you were using an inputStream:
inputStream.close();
}

try{
//code throwing exception
} catch (AmazonS3Exception) {}

Related

How can I find out why my app crashes if logcat show no reason?

I have an app that processing some pdf files in background and suddenly it crashes with no 'caused by' description. Tried with real devices, new emulators, restarted pc and so on.
Never had something before and I don't know where to search for the root cause.
Edit: I found that problem was in a try/catch block
try {
val pdfReader = PdfReader(URL(it))
//process
pdfReader.close()
} catch(e: FileNotFoundException){
//process exception
}
So the problem was in wrong exception, when I switched to a more general exception, I could handle it and see error in logcat
try {
val pdfReader = PdfReader(URL(it))
//process
pdfReader.close()
} catch(e: Exception){
//process exception
}
If you are using iTextPdf it can sometimes throw NoClassDefFoundError for no reason as far as i have experienced. Maybe catching this Error instead of Exceptions could help you identify the problem. Like this:
try
{
// do stuff
}
catch (Error err) //or NoClassDefFoundError
{
System.err.println(err.getMessage());
}

Hide thrown exception messages on Android

Is there a way to block the displaying of thrown exception messages (with all the stacktrace and everything) for an android app ?
EDIT:
Well, I know I can use try/catch block to do that that's not what I want.
Is there like a way to specify that in mafinest file or maybe in project settigs or smthng ?
you can wrap your code in try catch
try {
// code that might throw an exception
} catch (Exception e) {
//don't print the exception
}

How can I send an exception manually with Crittercism?

I'm using the android crittercism library and trying to send a report when I catched an error level exception.
But I can't find the documentation on how to do that. Is this even possible? If so, how?
They temporarily moved the feature into beta. If you e-mail support they'll enable your account for handled exceptions. Below is the sample Android code:
try
{
throw new Exception("Exception Reason");
}
catch (Exception exception)
{
Crittercism.logHandledException(exception);
}
Just in case you need it, here is sample code on iOS:
#try {
[NSException raise:NSInvalidArgumentException
format:#"Foo must not be nil"];
} #catch (NSException *exception) {
// Pass it on to us!
[Crittercism logHandledException:exception]
}
I'm the co-founder and CTO of Crittercism. If you send me an awesome email, I can enable it for your account. I'm rob[at] :)
Crittercism.logHandledException(new Throwable("test"));
You don't actually have to throw the Exception (or Throwable, in this case).
It will appear under "Handled Exceptions" on Crittercism website.

Catch all exceptions and send them by e-mail

In my app I want to catch all types of exceptions and send reports by e-mail. For that I'm using global try catch block. But now I need to recognize exception by type. How can I do it?
try{
...
}
catch (Exception e){
//Here I need to recognize exception by type
send(Error);
}
Why you don't simple send the whole stacktrace?
send(e.getStackTrace())
It not only contains the Exception type but also where (file, class, line) it occurred.
Additionally, you can also simply use the toString() method.
See the java doc for further information
Instead of rolling your own error logging and reporting mechninism I strongly recommend you use ACRA Its free, open source, and supports sending error logs to email. I have used it for quite some time and it is very good.
This will give you all sorts of information such as phone make, model, resolution, free memory, as well as a full stack trace of the error. Its by far the easiest way to get quality error reporting into an Android app.
The best part is it takes all of about 5 minutes to get setup and integrated.
e.getClass() // will give you Class object
e.getClass().getName() // will give you class name
However if you know the class names already you can use
if(e instanceof A)
{
// some processing
}
else if(e instanceof B)
{
//some processing
}
else
{
//
}

How to catch IOException Error in eclispe for Android?

I want to catch an IOException Error and want to show it in the form of toast for android application development. But when ever i apply catch(IOexception e), after try block it says to throws (throws IOException) with function name after which i am to catch error message.. Please Provide Some assistance....
May be you desire to 'throws' catched exception above the call stack, and next, catch and process all exception in one place (or 'layer') of code?)

Categories

Resources