1

I have exception handling being done on every iteration of a while loop. For some reason, when the condition for throwing an exception is true, the exception message is displayed but the program keeps running without interruption. I have never seen this happen before and suspect it is because of the while loop. I MUST check on every iteration for the existence of a null object in my Queue. Another thread suggested using a try clause, so I changed my code to use try clause, but I am still getting the same thing. The exception is thrown but the program does not crash. What is causing this?

Purchase ownedShares;
while (numSharesToSell > 0) {
  try  {
    ownedShares = q.peek();
  }
  catch(NullPointerException ex) {
    throw new IllegalArgumentException("Not enough shares");
  }

EDIT 2:

Using isEmpty() does not work either:

Purchase ownedShares;
while (numSharesToSell > 0) {
  if (q.isEmpty())
    throw new IllegalArgumentException("Not enough shares");
  else
    ownedShares = q.peek();
 // try  {

The core error here is not that the exception is not being executed. It IS being executed, and the message "Not enough shares" IS being printed. The problem is that when this occurs the program does not halt. The outer loop continues.

EDIT 3:

The method call I am modifying is wrapped in a try/catch block which prevents any errors I throw from crashing the program. This leaves me to conclude that the person who gave me the skeleton class for this code does not want exceptions to crash the program.

Community
  • 1
  • 1
chopper draw lion4
  • 12,401
  • 13
  • 53
  • 100
  • 1
    if `numSharesToSell` is larger than 0, it looks like you have an infinite loop, unless you left out the part where you update `numSharesToSell` – nem035 Oct 20 '14 at 23:57
  • 1
    @nem - incorrect. `q` being null and `q` being empty are two very different things. this line will not throw an NPE if `q` is empty, `ownedshares` will just be null. only if `q` is null will an NPE be thrown. – drew moore Oct 20 '14 at 23:59
  • @drewmoore i realized that i misunderstood him and updated the comment – nem035 Oct 21 '14 at 00:00
  • @nem, just tried it with isEmpty() and posted results. Same thing – chopper draw lion4 Oct 21 '14 at 00:01
  • It's impossible for the loop to continue if you throw an exception from it. My bet is that you have omitted something important from the code you've shown us or are misunderstanding something. Exceptions also do not always crash the program. It is possible it is caught somewhere higher up that you do not see or the application is multithreaded and only the worker thread dies. – Radiodef Oct 21 '14 at 00:03

1 Answers1

5

If an exception message is being printed but the program does not crash, that implies that code somewhere up the call stack is catching that exception, printing its message, and then swallowing it. That is, there is a try-catch block in a caller somewhere.

You should use the debugger to break inside your loop and look at the call stack, then examine the code for all the calling functions to find the try-catch.

merlin2011
  • 71,677
  • 44
  • 195
  • 329