views:

301

answers:

5

In a Java program I am currently getting "Unhandled Exception" at a certain point in the program and I can't seem to determine the location where this is being generated.

Its also difficult to debug the code as the program contains streams that handle wireless data bytes sent and received. I can't seem to simulate that with a debugger.

What strategy should I adopt to locate the Exception?

+1  A: 

If you don't have a stacktrace, you can't do much.

If it is actually caused at the other side and you received this as a message, then you should ask the other side for a stacktrace or similar information.

Edit: you should of course ensure that your own code isn't swallowing exceptions like as:

} catch (Exception e) {
    // Do nothing.
}

or

} catch (Exception e) {
    System.out.println("Error");
}

or

} catch (Exception e) {
    System.out.println(e.toString());
}

To get the most information out of exceptions, you should do at least:

} catch (Exception e) {
    e.printStackTrace();
}

or just

} catch (Exception e) {
    throw e;
}

Hope this helps.

BalusC
+1  A: 

You could put a try-catch(Exception ex) block around sections of code and move it around/tighten the block and have it log the exception being thrown. As long as there isn't too much code you should be able to track it down in a few runs.

Dan McGrath
Well I didnt handle a particular instance in a try catch block, I was only handling IO Exceptions while some other types were also getting thrown, so had to add another catch() and the problem resolved itself!
Kevin Boyd
@Kevin - I don't know if this is what you did ... but it is a really, bad idea to simply catch and ignore exceptions. An exception is telling you that something is wrong; e.g. a bug in your program. You should investigate the cause and (most likely) fix it.
Stephen C
Yes. The non-handling try-catch block idea was just to work out where you had issues with your program if you truly couldn't debug. From there you should be able to work out was was going wrong and fix your code.
Dan McGrath
@Stephen C and Dan McG: Well i logged the error in the new catch() and I have explained to jitter in a comment to my question above that what I did. What else should I do in the new catch, should also try to correct the error.
Kevin Boyd
+9  A: 

Implement the Thread.UncaughtExceptionHandler interface and use setDefaultUncaughtExceptionHandler() to set it.

Sample program as courtesy. If you use multiple threads you also could just set the handler on threads you suspect to be the culprits.

public class Test {
  public static void main(String args[]) {
      new Test();
  }
  public Test() {
      Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandler());
      // or if a default handler is set you can also use setUncaughtExceptionHandler
      // check the javadocs for the specifics
      throw new RuntimeException ("You can't get me or can you?");
  }
  class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
      public void uncaughtException(Thread t, Throwable e) {
          System.err.println ("Uncaught exception by " + t + " caught:");
          e.printStackTrace();
      }
  }
}
jitter
Are Uncaught Exceptions checked or unchecked exceptions?
Kevin Boyd
Presumably they'd be unchecked exceptions. Checked exceptions couldn't propagate beyond a `try { ... }` block.
harto
Most of the time they will be unchecked exceptions but depending on your concrete implementation they might also be checked exceptions. e.g. if you have checked exceptions which you don't handle with try-catch but just declare them in the throws clause up to e.g. main()
jitter
Btw it now gets to me that you may be talking about Java ME. Then you might need to adopt the MyUncaughtExceptionHandler sample to be able to see the actual output if there is no stdout available. You could look into microlog or logMEadvanced for logging while developing on mobile devices
jitter
@jitter: Yes it is JavaME, and thanks will look into microlog. Right In now I have just logged errors into a Vector.
Kevin Boyd
+1  A: 

But as I run the program the device shows "Unhandled Exception" and asks me whether to close the app.

First you need to find the place in the code that is generating this message. It sounds like the app has a GUI or whatever, so it is possible a Dialog of some kind.

Next, find the place in the code that is causing the message/dialog to be created. It is likely to be either

  • a try / catch block that catches Exception or Throwable, or
  • an UncaughtExceptionHandler.

In either case, the next thing is to add some code to cause the app to output a stacktrace for the uncaught exception. Assuming that ex holds a reference to the exception object:

    ex.printStackTrace();

will write a stack trace to the standard error stream; e.g. the "console". (There are more heavy-weight solutions if you cannot find where "console" output goes to.)

Stephen C
+1  A: 

Sounds like you've got an unchecked RuntimeException happening somewhere. You could easily try it in your main() method with try { } catch(Throwable t) { t.printStackTrace(); }

Or if you remotely debug it with an IDE like Eclipse, you can set it up to trigger the debugger on a Java exception breakpoint with "Suspend on uncaught exceptions". Some docs here.

Alex Miller