0

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

Community
  • 1
  • 1
Amos
  • 1,321
  • 2
  • 23
  • 44
  • Please post your code and the logcat output. – code monkey Dec 25 '14 at 15:12
  • The code is exactly as shown here: https://developers.facebook.com/docs/android/login-with-facebook/v2.2 but my question is general, I want to know how to generally catch any kind of exception no matter where it happens. I want one place to catch any kind of exception. Thanks. – Amos Dec 25 '14 at 15:20

3 Answers3

1

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!

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
code monkey
  • 2,094
  • 3
  • 23
  • 26
  • I'm not talking about a general exception type handler, I'm looking for one place to catch them all and there I can distinguish the actual exception type and act accordingly. I know it's bad practice but that's the only way for me now, to catch the place where it happens because a simple try/catch clauses just won't do. – Amos Dec 25 '14 at 15:37
1

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));
Liran Peretz
  • 580
  • 8
  • 11
0

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.

Tor_Gash
  • 164
  • 1
  • 9
  • I tried this and also logged to a file all the steps that happen. When it finishes the "onResume" event, it throws an exception but it seems to happen from outside my actual code. If I can have 1 place to catch whatever exception that happens in my app, it will be very helpful. – Amos Dec 25 '14 at 15:28
  • I may disappoint you, but exceptions are thrown by methods. The point where exception is thrown is the breakpoint; this means that the exception must be handled right here, right now; no one other piece of code can do this. So whatever your methods are and whatever exceptions they throw, every method potentionally throwing exceptions must be surrounded with try/catch. Also keep in mind that handling exceptions by base Exception class is the last thing to do. Handle them by their type corresponding to stacktrace output. – Tor_Gash Dec 25 '14 at 15:34
  • wait, you said "outside my actual code". Maybe that's a runtime exception? then I need your full stacktrace, nothing else to say. – Tor_Gash Dec 25 '14 at 15:38
  • I want that too and that's exactly my question, how can I get it when running on my device and the exception is not caught inside any of my try catch clauses? – Amos Dec 25 '14 at 15:41
  • you should check if this is a runtime exception or even an error. Errors can't be handled in code, you can only change methods to avoid them. If you need help, provide the stacktrace of your unknown exception – Tor_Gash Dec 25 '14 at 15:50