Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Global Exception Handling on android

Is there a code example, or a tutorial on how to use the Thread.setDefaultUncaughtExceptionHandler method? Basically I'm trying to display a custom alert dialog, whenever an exception is thrown, in my application. Is it possible to do this? I know it's a little bit tricky to display something on the screen, if the exception is thrown in the UI thread but I don't know any work around for this.

like image 791
Gratzi Avatar asked Dec 13 '10 09:12

Gratzi


People also ask

How are exceptions handled in Android?

How Exceptions Work in JVM and Android. In Java, all exception and error types are subclasses of Throwable. The Throwable class changes the execution flow of JVM apps by throwing exceptions and deciding how to recover from an error. Adding an exception to a code changes its execution flow.

How do you handle global exception?

In the Design tab part of the Ribbon, select New > Global Handler. The New Global Handler window opens. Type in a Name for the handler and save it in the project path. Click Create, a Global Exception Handler is added to the automation project.

How do you handle global exception in Microservices?

First, we need to set up the capability of throwing exceptions on core banking service errors. Open core banking service and follow the steps. Create a common exception class where we going to extend RuntimeException. After that, we can create custom runtime exceptions to use with this API.


2 Answers

Basic Example for someone who comes to this page with a solution :)

public class ChildActivity extends BaseActivity {     @SuppressWarnings("unused")     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         int a=1/0;     } } 

Class for handling error:

public class BaseActivity extends Activity{     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {             @Override             public void uncaughtException(Thread paramThread, Throwable paramThrowable) {                 Log.e("Alert","Lets See if it Works !!!");             }         });     } } 
like image 184
Code_Life Avatar answered Sep 23 '22 20:09

Code_Life


Here's a variant of the answer by Mohit Sharma with the following improvements:

  • Doesn't cause the app/service to freeze after error handling
  • Lets Android do its normal error handling after your own

Code:

public class BaseActivity extends Activity {     @Override     public void onCreate() {         super.onCreate();          final Thread.UncaughtExceptionHandler oldHandler =             Thread.getDefaultUncaughtExceptionHandler();          Thread.setDefaultUncaughtExceptionHandler(             new Thread.UncaughtExceptionHandler() {                 @Override                 public void uncaughtException(                     Thread paramThread,                     Throwable paramThrowable                 ) {                     //Do your own error handling here                      if (oldHandler != null)                         oldHandler.uncaughtException(                             paramThread,                             paramThrowable                         ); //Delegates to Android's error handling                     else                         System.exit(2); //Prevents the service/app from freezing                 }             });     } } 
like image 23
Sam Avatar answered Sep 20 '22 20:09

Sam