Can I call a method before my application crashes

I am new to android and I always see Exception when I run my code. So, can someone tell me if I can call the method before the application goes to any point without try-catch.

+1
source share
5 answers

This would be the best way to handle an uncaught exception:

public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); appInitialization(); } private void appInitialization() { defaultUEH = Thread.getDefaultUncaughtExceptionHandler(); Thread.setDefaultUncaughtExceptionHandler(_unCaughtExceptionHandler); } private UncaughtExceptionHandler defaultUEH; // handler listener private Thread.UncaughtExceptionHandler _unCaughtExceptionHandler = new Thread.UncaughtExceptionHandler() { @Override public void uncaughtException(Thread thread, Throwable ex) { ex.printStackTrace(); // TODO handle exception here } }; } 

The application is the base class for those who need to maintain the state of the global application. And therefore, there will be a better place to handle such exceptions.

EDIT: The above code will handle uncaught exceptions if they are thrown into the user interface thread. If an exception occurs in the workflow, you can handle it as follows:

 private boolean isUIThread(){ return Looper.getMainLooper().getThread() == Thread.currentThread(); } // Setup handler for uncaught exceptions. Thread.setDefaultUncaughtExceptionHandler (new Thread.UncaughtExceptionHandler() { @Override public void uncaughtException (Thread thread, Throwable e) { handleUncaughtException (thread, e); } }); public void handleUncaughtException(Thread thread, Throwable e) { e.printStackTrace(); // not all Android versions will print the stack trace automatically if (isUIThread()) { // exception occurred from UI thread invokeSomeActivity(); } else { //handle non UI thread throw uncaught exception new Handler(Looper.getMainLooper()).post(new Runnable() { @Override public void run() { invokeSomeActivity(); } }); } } 
+8
source

I think you are looking for an UncaughtExceptionHandler. It is notified whenever an Exception is triggered and does not fall into its path bubbling through your application.

See http://www.intertech.com/Blog/android-handling-the-unexpected/ for more on android.

0
source

Try this way

1) create a class

 import android.content.Context; import android.content.Intent; import android.os.Process; import java.io.PrintWriter; import java.io.StringWriter; import java.lang.Thread.UncaughtExceptionHandler; public class CrashReportHandler implements UncaughtExceptionHandler { public static void attach(Context context) { Thread.setDefaultUncaughtExceptionHandler( new CrashReportHandler(context) ); } ///////////////////////////////////////////// implementation private CrashReportHandler(Context context) { m_context = context; } public void uncaughtException(Thread thread, Throwable exception) { StringWriter stackTrace = new StringWriter(); exception.printStackTrace(new PrintWriter(stackTrace)); //You will get call back here when app crashes. // from RuntimeInit.crash() Process.killProcess(Process.myPid()); System.exit(10); } private Context m_context; } 

How to use this class?

write this line in your onCreate() method action

 CrashReportHandler.attach(this); 
0
source


There is a method called Uncaught exception, which is called immediately before the power closing dialog, you can write ur code for the code there. Please check Using global exception handling on android

0
source

Use CrashLytics for emergency reporter without cost and easy to implement

https://www.crashlytics.com/

0
source

All Articles