Reputation:
I have a for loop:
for(Location l : locs) {
System.out.println("X:"+l.getX()+", Y:"+l.getY());
try {
if(layer.getObject(l) != null)
out.add(layer.getObject(l));
} catch(NullPointerException e) {
}
}
Each iteration, it takes a Location from the Location[]
array, and prints out the X and Y of that location. Then, the next line gets the object from the location in a map layer (not the standard map library), and adds it to an ArrayList
.
I'm getting a java.lang.ClassCastException
, but that's not my question. That issue I can resolve on my own. My quesiton is why the output appears like this (notably, the way the exception is displayed):
run:
X:0, Y:1
X:0, Y:2
X:0, Y:3
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [LMapGrid.OverlayObject;
X:1, Y:1
X:1, Y:3
X:2, Y:1
X:2, Y:2
X:2, Y:3
at MapGrid.MapGrid.getAdjacentOfType(MapGrid.java:86)
at BehaviorMap.BehaviorMap.main(BehaviorMap.java:26)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
The object it's finding is at location (1,1), thus, the Exception was thrown at location (1,1). This is set from the beginning of the program. The program is running on the main thread. My question, though, is why does the output appear this way?
The way this is laid out would seem to imply that the Exception was thrown when the program reached (0,3), but the exception must have been thrown when it reached (1,1). Then, the output would imply that, once the exception was found, the program continued to execute until the loop exited.
I don't understand this behavior. When an unhandled exception is encountered, why doesn't the program simply exit? Additionally, why does the Exception message appear before the line invoking the Exception? Note that the System.out.println
comes before the try
block.
Lastly, why does the error split in such an odd way? (In that the error message appears, then the details appear after the program appears to continue execution.) Platform is NetBeans 7.2.
Upvotes: 3
Views: 81
Reputation: 6718
The way this is laid out would seem to imply that the Exception was thrown when the program reached (0,3), but the exception must have been thrown when it reached (1,1). Then, the output would imply that, once the exception was found, the program continued to execute until the loop exited.
The default behaviour for uncaught exceptions is to print them to the standard error stream (System.err
). This can be displayed on the console in a different order to the standard output stream (System.out
).
I don't understand this behavior. When an unhandled exception is encountered, why doesn't the program simply exit? Additionally, why does the Exception message appear before the line invoking the Exception? Note that the System.out.println comes before the try block.
It depends what MapGrid.MapGrid.getAdjacentOfType
is doing. It looks like it's catching the exception and calling Exception.printStackTrace()
, which prints to standard error.
Lastly, why does the error split in such an odd way? Being that the error message appears, then the details appear after the program appears to continue execution. Platform is NetBeans 7.2.
Again, because the output and error streams are not necessarily displayed on the console in the same order they are written to (relative to each other).
Upvotes: 3