Exception details before program termination?

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 takes a location from an array Location[]

and outputs the X and Y of that location. Then the next line gets the feature from a location on the map layer (not in the standard map library) and adds it to ArrayList

.

I receive java.lang.ClassCastException

, but that's not my question. I can solve this question on my own. My quesiton is why the output looks like this (specifically, how 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 finds is at location (1,1), so an Exception was thrown at location (1,1). This is installed from the beginning of the program. The program runs in the main thread. My question is, however, why does the output look like this?

As stated, it would seem to imply that the Exception was thrown when the program reached (0.3), but the exception should have been thrown when it reached (1,1). Then the output would mean that after the exception was caught, the program continued to run until the loop ended.

I don't understand this behavior. When an unhandled exception is encountered, why doesn't the program exit? Also, why does the exception message appear before the line that is causing the exception? Notice what System.out.println

precedes the block try

.

Finally, why is the error crashing in such a weird way? (If you receive an error message, you are informed that the program is popping up to continue execution.) The platform is NetBeans 7.2.

+3


source to share


1 answer


As stated, it would seem to imply that the Exception was thrown when the program reached (0.3), but the exception should have been thrown when it reached (1,1). Then the output would mean that after the exception was caught, the program continued to run until the loop ended.

The default behavior for thrown exceptions is to print them to standard error ( System.err

). This may appear 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 exit? Also, why does the exception message appear before the line that is causing the exception? Note that System.out.println comes before the try block.



It depends on what it is doing MapGrid.MapGrid.getAdjacentOfType

. It looks like it is catching the exception and calling Exception.printStackTrace()

, which prints the standard error.

Finally, why is the error crashing in such a weird way? When an error message appears, information appears after the program continues. The platform is NetBeans 7.2.

Again, since the output and error streams are not necessarily displayed in the same order on the console, they are written (relative to each other).

+3


source







All Articles