Android thread exception - android

I have a program that works with multiple threads.
For each I set uncaught exception handler th.setUncaughtExceptionHandler (new AppExceptionInterloper ()), but it displays "thread exiting with uncaught exception (group = 0x4001d800)" as a localized problem. Why it appears at the log and not in the class AppExceptionInterloper?

same error i have got just now. You must have forgotten to catch the ecxeption in any try-catch like Arrayindexoutofbound or JSONecxeption like wise. I have used the array and got the JSON as the result from the webservice. I forgot to put the JSONEcxeption in it.when i caught it.
When you see the stack trace you will find the "Caused by: java.lang.ClassCastException" or some thing like another exception in your case. Do catch it. You will get your code run.

Related

Error handling in the onCreate() method of activity android

I am new to android development. In the overriden onCreate() method of my activity, I perform some operations. For ex. check if SharedPreference is already available and then route to other activity. How do I perform exception handling on this onCreate() method. Is it the right way to wrap the contents in a try catch and display the error dialog on exception?
If the exceptions are not handled properly, in my case the onCreate() method, the app crashes with message:
Unfortunately your application stopped working
On searching in the internet, I found that UncaughtExceptionHandler could be used to handle it. Any sample implementation and how to call it from all my activities would be helpful.
THat's the wrong way to go about it. Instead, go into your logcat. Read the stack trace of the exception. Figure out what you did wrong. Fix it. Just catching exceptions is a horrible practice unless you're catching a specific exception for a specific reason- its very unlikely your app is in a state where it can continue correctly. Don't be lazy, track down your bug and fix it.
With something like this you can catch a generic Exception in your onCreate() method:
#Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
try {
// do whatever you need
} catch (Exception e) {
e.printStackTrace();
}
}
Exception catching can be used with many goals. An interesting one is to catch particular expected exceptions to fix some problems (for example setting a generic value into a variable otherwise null after occurring an error).
The general purpose however is to avoid that an error causes a problem or in the worst case a crash of your application. Depending oh what kind of software you are developing, a crash may represents a little problem (like in an Android app), a very big one (like in airplane's softwares or power plants' softwares).
We can identify two kind of exceptions.
The first type are those exceptions specific to definite problems. They have to be declared into methods signature, so you are encouraged or obbligated to manage these exceptions and their relative problems. These exceptions represent an error that should probably occur during particular code execution, like a parsing error, or an input output error. For this type of problems we have particular exceptions, to catch and easily manage them (for example as previously said to init a variable with a default value after an error).
The second type of those exception represents instead some generic problems that can occur during program execution. They are a big and generic set and generally their probability of occurring is low. For this reason you are not obbligated to manage them. The main example of this type is the general Exception class.
So an catching an Exception is not the right approach to solve a bug, as suggested by Gabe Sechan.
Catching a generic Exception in the main() Java method can be a way, for example, to show a default human readable error to the user if nothing else is available.
Or could be a way to keep the failing of the current operation reducing crash probability.
Now, in Android we can't act directly on the main() method. onCreate() method is executed and exited so catching here a generic Exception has no much sense. Obviously it depends also on what you do in your onCreate() method and what you want to do if an error occurs. You could do some strange stuff (is very defined what you should do in the onCreate() method) and you could need to manage a big set of problems only showing an error, so it has more sense catch only a generic Exception than a lot of particular exceptions to make the same thing in each catch block.
If you want to achieve this goal (a message showed for all the errors occurred during onCreate() execution, made Activity by Activity) this is the right approach.
If instead what you want to achieve is to intercept all of the errors that are generated during all of your app execution, a good approach could be the UncaughtExceptionHandler, that act similarly to catch a generic Excepetion into a Java main() method. At this level an interesting approach is described here.
For more infos about exceptions you can read this and this.

Exception on android side

I'm using crashlytycs and recently i begin to get such exception, but i don't know how to reproduce it on my application.
I know how to fix it. The fix is to use runOnUiThread, but i don't know the point where this exception happens.
When i got exception, i see the line of it in logs, but this one is different, because exception occurs in android class, not mine. Can you give me some advices about where can i search for this exception. I'm not changing ui in asyncTask background. I use volley for networking.
Error Log:
Fatal Exception: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:7406)
at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:1113)
at android.view.ViewGroup.invalidateChild(ViewGroup.java:4595)
at android.view.View.invalidate(View.java:11301)
at android.view.View.invalidate(View.java:11250)
at android.widget.TextView.updateAfterEdit(TextView.java:7817)
at android.widget.Editor.finishBatchEdit(Editor.java:1330)
at android.widget.Editor.endBatchEdit(Editor.java:1313)
at android.widget.TextView.endBatchEdit(TextView.java:6084)
at com.android.internal.widget.EditableInputConnection.endBatchEdit(EditableInputConnection.java:82)
at android.view.inputmethod.BaseInputConnection.finishComposingText(BaseInputConnection.java:292)
at com.android.internal.widget.EditableInputConnection.finishComposingText(EditableInputConnection.java:199)
at android.view.inputmethod.InputMethodManager.checkFocusNoStartInput(InputMethodManager.java:1448)
at android.view.inputmethod.InputMethodManager.onWindowFocus(InputMethodManager.java:1500)
at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:4211)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.os.HandlerThread.run(HandlerThread.java:61)

Writing to log causes App to crash?

I was going through following tutorial: http://www.dreamincode.net/forums/topic/190013-creating-simple-file-chooser/#viewSource
Inside the FileChooser class there is one exception beeing supressed, since there is nothing within the catch clause. So I decided to add following statement to the catch clause:
Log.e(ACTIVITY_SERVICE, e.getCause().toString());
For some reason this always causes the app to crash (NullPointerException). When I don't do any logging, except for a a simple syso print, then I don't get any exception and the app does't crash either. Then it works fine. I'm new to the whole Android Framework and just want to understand why this is happening.
Thanks to Ingo's and CommonsWare's hints I came to the conclusion, that following line caused the exception:
EDIT:
In fact it was the start of the foreach loop, which iterates over all subdirectories and files of a given directory. I didn't know, that this would cause a NPE, if the list you want to iterate on is actually null. For some reason I thought that the loop would be automatically skipped in this case.

How to pause Eclipse on any exception?

I found the "Add Java Exception Breakpoint" menu item but it only seems to work on the exact exception type that I select. So if I ask it to break on Exception, it does not break in case of a NumberFormatException. How do I make it break for all exceptions?
My activity is exiting for no apparent reason with no LogCat output, so it would be nice to find out about any exceptions that are occurring, whether caught or uncaught and whether in my code or just in Android.
create an exception breakpoint for java.lang.Throwable (which is the most specific superclass of all exceptions - unlike Exception, it also matches instances of Error)
right-click it, choose properties, and check "subclasses of this exception"
You should be able to do this using the pattern matching it specifies in the window. Specifically, * will match any string (including the empty string) so *Exception* will match all strings which contain the substring Exception in them, including the string Exception itself and strings which have Exception at the very beginning or very end.

Eclipse Logcat window cuts off exception stack traces

My logcat window in Eclipse only displays the first few lines of the StackTrace for each exception. This means that I often can't see where an exception occured. Is there any way to change this setting?
If you're referring to the "...12 more lines..." part, you only see that for exceptions that were the cause of another exception. If the top part of the stack trace is the same as the earlier trace, the full set of frames is only shown for the outermost exception, and the other traces get the "..." treatment.
Put another way, the chunk of a trace that isn't shown is a duplicate of a trace that appeared earlier in the exception cause chain. For example, suppose I have code where the method main() calls one(), which calls two(), and so on. four() throws an exception. two() catches it and re-throws it. The exception will look like this:
java.lang.RuntimeException: re-throw
at Foo.two(Foo.java:14)
at Foo.one(Foo.java:7)
at Foo.main(Foo.java:3)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: first
at Foo.four(Foo.java:23)
at Foo.three(Foo.java:19)
at Foo.two(Foo.java:12)
... 3 more
The "caused by" exception says "... 3 more" rather than explicitly listing one(), main(), and dalvik.system.NativeStart.main. So to get the full trace of the initial exception, you would start by reading its trace, then continue at the trace above.
Note there is no overlap -- two() appears in both, but in the "first" trace it's on the call to three(), and in the "re-throw" trace it's on the throw instruction.
you can overload all the log methods (log.d, log.i, log.e, etc) with (String tag, String msg, Throwable tr) parameters, where the third parameter is the exception. This will give you the full stacktrace in logcat
http://developer.android.com/reference/android/util/Log.html
If your code calls a method which produces too tall of stack you can (and should) handle the exception in your code and output whatever is relevant to logs.
If you have no exception handling whatsoever and you don't even know where in your code should you be putting such a handler then the problem is entirely elsewhere - you should be handling exceptions a bit better than that.

Categories

Resources