Android Exceptions Without Forced Closing

I might be missing something, but whenever an Exception is thrown in Android (or java), my ALLWAYS application is forced to close and the whole program terminates. However, when something goes wrong, for example, in a database query, I just want to return to the main menu.

try { database.query(params); } catch (Exception e) { Log.e("Game", "Failed Loading Level", e); returnToMenu(); } 

}

This, for example, forcibly closes my program, I just want it to continue!

0
source share
3 answers

All Android developers must face a serious problem when developing an application. Here is a method to catch this error and treat it elegantly.

This will create an error page type mechanism in your Android app. Therefore, whenever your application crashes, the user will not be able to see this annoying pop-up dialog. Instead, the application displays a preview of the user.

To create such a mechanism, we need to make one error handler and the Activity class, which will get a view when the application is forcibly closed.

 import java.io.*; import android.content.*; import android.os.Process; public class ExceptionHandler implements java.lang.Thread.UncaughtExceptionHandler { private final Context myContext; public UncaughtExceptionHandler(Context context) { myContext = context; } public void uncaughtException(Thread thread, Throwable exception) { StringWriter stackTrace = new StringWriter(); exception.printStackTrace(new PrintWriter(stackTrace)); System.err.println(stackTrace); Intent intent = new Intent(myContext, CrashActivity.class); intent.putExtra(BugReportActivity.STACKTRACE, stackTrace.toString()); myContext.startActivity(intent); Process.killProcess(Process.myPid()); System.exit(10); }} 

The above class will work as a listener for a forced close error. You can see that Intent and startActivity are used to start a new action every time the application crashes. Thus, he will start working with the name CrashActivity whenever the application is broken. At the moment, I went through the stack trace as additional jobs.

Now, since CrashActivity is a regualr of Android Actitvity, you can handle it in any way.

Now comes the important part, that is, how to catch this exception. Although it is very simple. Copy the following line of code in each of your activities immediately after calling the super method in your override onCreate method.

Thread.setDefaultUncaughtExceptionHandler (new UncaughtExceptionHandler (this));

Your activity might look something like this ...

 public class ForceClose extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler(this)); setContentView(R.layout.main); // Your mechanism is ready now.. In this activity from anywhere if you get force close error it will be redirected to the CrashActivity. }} 

you can download the zip file from this link

http://trivedihardik.wordpress.com/2011/08/20/how-to-avoid-force-close-error-in-android/

+7
source

If you implemented onSaveInstanceState () and onRestoreInstanceState (), you could replace CrashActivity.class with the activity that caused the crash. See http://developer.android.com/reference/android/app/Activity.html .

0
source

I had a similar problem and ended up on this page because I couldn’t understand why my application didn’t catch the exception that I threw. It turns out that this was for an obvious reason, so I will share it.

In my case, I had code that received JSON data from an HTTPS request. This made the compiler whine, and I had to try / catch the code parsing JSON. So far, so good.

JSON contained, among other things, version strings in an arbitrary format, and I wanted to compare this version string with the firmware version on the device to which the application is connected. So I created a class that takes the version string and parses it into various components, and then uses them in a specific order for comparison. In the constructor, where I parse the string into components, I throw an exception if the string is invalid. Again, this is normal and completely normal.

The guys who work on the firmware tell me that my application crashes. OK - you need to find out why. Turns out they put the wrong version string in the firmware. OK is good. What should happen is that the Version class throws an exception that is caught by my try / catch and simply prevents the logic associated with comparing the versions and does not display the available firmware update, which is normal in this (unusual) situation.

After I tried to understand why the exception was not caught, I finally realized that this is because my try / catch is catching a JSONException, while my Version class throws a RuntimeException.

So if you cannot understand why your try / catch does not catch the exception, you think that it should catch, and you get here, double-check what type of exception you are dealing with, and make sure you have the appropriate type for catching.

0
source

Source: https://habr.com/ru/post/984990/


All Articles