Amos
Amos

Reputation: 1341

Android - general exception catcher

I'm trying to integrate Facebook into my app so I followed all the instructions. When running it, after onResume event finishes, I get an exception that I can't catch from my code.

Is there a way to set a general exception catcher so no matter what, it will catch it?

The emulator does not throw any exception at all so I can't use it for this

Update: I found it here: Using Global Exception Handling on android

The error came from Facebook itself and it is related to working with proguard

Upvotes: 0

Views: 1337

Answers (3)

code monkey
code monkey

Reputation: 2124

The Throwable class is the superclass of all errors and exceptions in the Java language.

If you catch a throwable you can catch all kind of errors and exceptions.

try {
    // code
} catch(Throwable e) {
    // handle throwable
}

However this is strongly not recommended and bad practise/design. You should never catch generic errors and exceptions. You should analyse your specific exception and solve the problem instead of simply ignoring it with try/catch!

Upvotes: 1

Liran Peretz
Liran Peretz

Reputation: 580

try this solution:

  1. create a class that implement Thread.UncaughtExceptionHandler (this class going to handle with the uncaughtExceptions), lets call this class ExceptionHandler.
  2. create a class that extends Application, lets call this class App, and don't forget to declare this in the manifest file under application tag: android:name="your_package_path.App"
  3. in your App class override the method onCreate(), and add this line: Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));

Upvotes: 1

Tor_Gash
Tor_Gash

Reputation: 164

There is a base Exception class. So if you do like this:

try {
  ...anything...
}catch(Exception e) {
 e.printStackTrace();
 }

you'll catch any exception that is thrown by any method called from within the try block. But remember that exceptions aren't always thrown. Sometimes there's just no result to return, or something.

Upvotes: 0

Related Questions