m in try block
Exception in thread "main" java.lang.ClassCastException // why does this prints before printing next line i.e. before array index out of bounds
array index out of bounds
i am always executed
Java Result: 1
when ArrayIndexOutOfBoundsException occurs the control goes to first catch block so it must first print "array index out of bounds" and then should throw ClassCastException. but it happens opposite. please help ?
Your System.out.println calls are written to standard output.
The Exception in thread "main" java.lang.ClassCastException message is written to standard error.
Because you are writing to different output streams and the i/o is asynchronous there is no guarantee of the order in which the messages will be written.
If you want to guarantee the order, change the System.out.println calls to System.err.println calls