Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catch all type exceptions programming Android

I'm developing an application for Android OS. Since this is my first application, I think I've committed some programming mistakes cause I hardly can trace bugs back to their causes. Thus, I was guessing, while i'm trying to fix bugs, is there a way to catch ALL types of exception in my entire activity lifecycle with one try-catch?

That would be awesome, i'm getting bored watching my galaxy S say :"Sorry the application App has stopped unexpectly" :(

like image 746
Archimedis Avatar asked Apr 24 '11 23:04

Archimedis


People also ask

How do you catch different types of exceptions?

The try-catch is the simplest method of handling exceptions. Put the code you want to run in the try block, and any Java exceptions that the code throws are caught by one or more catch blocks. This method will catch any type of Java exceptions that get thrown. This is the simplest mechanism for handling exceptions.

How many types of exceptions are there in Android?

Runtime exception thrown when an attempt is made to create a file system that already exists. Thrown when a file system operation fails on one or two files. Checked exception thrown when a file system loop, or cycle, is encountered. Runtime exception thrown when a file system cannot be found.

How do you use try catch on Android?

Try essentially asks Java to try and do something. If the operation is successful, then the program will continue running as normal. If it is unsuccessful, then you will have the option to reroute your code while also making a note of the exception. This happens in the “catch” block.

How do I get an exception message on android?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken textview to show exception message.


2 Answers

I really, really don't recommend this...

try {
     ...
} catch (Exception e) {
     // This will catch any exception, because they are all descended from Exception
}

Are you looking at your stack traces to debug your issues? It should not be hard to track them down. Look at LogCat and review the big block of red text to see which method caused your crash and what your error was.

If you catch all your errors this way, your program is not going to behave as expected, and you will not get error reports from Android Market when your users report them.

You can use an UncaughtExceptionHandler to possibly prevent some crashes. I use one, but only to print stack traces to a file, for when I'm debugging an app on a phone away from my computer. But I pass on the uncaught exception to the default Android UncaughtExceptionHandler after I've done that, because I want Android to be able to handle it correctly, and give the user the opportunity to send me a stack trace.

like image 102
Tenfour04 Avatar answered Oct 08 '22 01:10

Tenfour04


I'm assuming like pure java

try {

} catch(throwable t) {

}

But this is Very Bad Practice.

Also look at

Setting UncaughtException handler

like image 45
MJB Avatar answered Oct 08 '22 01:10

MJB