Tuesday, February 8, 2011

Re: [android-developers] Am I doing UncaughtExceptionHandler correctly?

Dialogs are tied to activities, and your code calls finish() on the
activity right after calling show() on the dialog.

More importantly, an uncaught exception handler is not meant to be used
instead of Java catch blocks.

If you have code that throws an exception to indicate failure, handle it
wherever that code is called:

try {
doStuff();
}
catch (DataLoadingException x) {
.....
}

where:

void doStuff() throws DataLoadingException {
}

Make sure your derive the exception from Exception and not Error, so the
compiler helps you put catch blocks where needed.

-- Kostya

08.02.2011 6:11, Mystique пишет:
> Hi, I want to intercept all exceptions with a global exception with a
> AlerDialog but my UncaughtExceptionHandler doesn't seems to show the
> dialog. Did I implement it correctly?
>
> Many Thanks.
>
> I put this in onCreate() ->
> Thread.setDefaultUncaughtExceptionHandler(onDirectoryException);
>
> ---
> The I implemented Thread.UncaughtExceptionHandler ->
>
> private Thread.UncaughtExceptionHandler onDirectoryException = new
> Thread.UncaughtExceptionHandler() {
> String LOG_TAG;
> @Override
> public void uncaughtException(Thread thread, Throwable ex) {
> // TODO Auto-generated method stub
> Log.e(LOG_TAG, "Directory parsing or data connection timeout", ex);
> new AlertDialog.Builder(getBaseContext())
> .setTitle("Information")
> .setMessage("Data connection timeout. Please try again later.")
> .setPositiveButton(android.R.string.ok, null)
> .setCancelable(false)
> .create()
> .show();
> finish();
> }
> };
>


--
Kostya Vasilyev -- WiFi Manager + pretty widget -- http://kmansoft.wordpress.com

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment