September 2015
Intermediate to advanced
250 pages
6h 40m
English
Unlike Java, we have to be mindful of the order in which we place the catch blocks. The Java compiler is more vigilant than Scala in this area. When attempting to handle exceptions, Java watches over the order in which we place multiple catch blocks. The following example will give us a compilation error:
| ExceptionHandling/JavaCatchOrder.java | |
| | //Java code—will not compile due to incorrect catch order |
| | |
| | public class JavaCatchOrder { |
| | public void catchOrderExample() { |
| | try { |
| | String str = "hello"; |
| | System.out.println(str.charAt(31)); |
| | } |
| | catch(Exception ex) { System.out.println("Exception caught"); } |
| | catch(StringIndexOutOfBoundsException ex) { //ERROR |
| | System.out.println("Invalid Index" ... |
Read now
Unlock full access