9

Why will the Android "Catch" handler not initialize an Exception object?

When an error occurs and my code is trying to initialize an Exception object it is always NULL.

The above doesn't make sense to me, because the exception should always initialize the Exception object - period. It should never be null if an error occurs.

I am fairly new to the Eclipse Android IDE/SDK, and I am sure I don't have everything set up 100%. However, this type of functionality would seem to me that it should work all the time, not after being set up.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
John
  • 91
  • 1
  • 2
  • 1
    Can you show the relevant part of your code? What evidence have you used to arrive at the conclusion that the exception is null? How did you observe this evidence? – Mark Byers Oct 16 '10 at 16:01
  • Hi, I have the below handler setup: catch(Exception ex) { System.out.println("ERROR:\n\n" + ex.toString()); } When I run debug, the exception is always NULL....regardless of what the error is. It never initializes – John Oct 16 '10 at 16:26
  • You have a break point within the catch block, and ex is null? I find that hard to believe... If ex was null, your print statement would throw a null pointer exception, is that what happens? – Cheryl Simon Oct 16 '10 at 17:41
  • Yes, a NULL pointer exception is thrown. The main invoked method is a HTTP call. I originally was getting a HOSTNOTFOUND exception because my LAN connection was not working. However, when I started Eclipse today, the exception is not being thrown any longer. The Exception object is just not initializing. Any idea why this is happening? – John Oct 17 '10 at 03:16
  • Good Lord!!....does nobody have any idea why this error is occurring? Has nobody ever experience this issue besides me? – John Oct 20 '10 at 04:04
  • I'm having the same stupid error right now. How come something can throw null? This is definitely a Dalvik error. – Martín Schonaker May 13 '11 at 16:56
  • I still see this happening once in awhile. Is rather unhelpful! – melodiouscode Oct 13 '12 at 19:52

1 Answers1

6

One possible cause is that you are trying to make a network connection on your main thread, which works fine pre-2.3.3/Honeycomb (SDK level < 10 for example) but will be thrown as an

android.os.NetworkOnMainThreadException

since SDK level 10.

Check this: http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html

The problem is that Eclipse doesn't really know about this exception (since it is conditionally thrown based on different SDK level so Eclipse probably can't get a correct instance of this exception, that explains why your exception object is always NULL)

Solution: create a separate thread or use AsyncTask to perform your network connection request.

user789651
  • 151
  • 2
  • 2
  • How is it possible to invoke the exception handler without an instance of the exception? The exception handler code must effectively test `thrownException instanceof CatchException`, and without an instance of the exception that's gonna fail. – Hot Licks Sep 25 '12 at 23:21