Click here to watch in Youtube :
https://www.youtube.com/watch?v=pCbAsg0dUeE&list=UUhwKlOVR041tngjerWxVccw
Click the below Image to Enlarge
Java Tutorial : Java unchecked exception(ArrayIndexOutOfBoundsException) |
/* * If you compile this code, it would compile * successfully however when you will run it, it would * throw ArrayIndexOutOfBoundsException. That clearly shows that * unchecked exceptions are not checked at compile-time, * they are being checked at runtime. */ public class UnCheckedExceptionDemo1 { public static void main(String[] args) { int intArray[] = { 1, 2, 3 }; /* * intArray has only 3 elements but I'm trying to * display the value of 6th element. It should throw * ArrayIndexOutOfBoundsException */ System.out.println(intArray[5]); System.out.println("Normal flow..."); } }
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 at UnCheckedExceptionDemo1.main(UnCheckedExceptionDemo1.java:20)
import java.util.Scanner; public class UnCheckedExceptionDemo2 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the index: "); int index = scanner.nextInt(); scanner.close(); try { int intArray[] = { 155, 345, 999 }; System.out.println(intArray[index]); } catch (ArrayIndexOutOfBoundsException arrayIndexOutOfBoundsException) { System.out.println("Enter the index value less " + "than or equal to 2,\nbecause " + "size of the array is 3"); } System.out.println("Normal flow..."); } }
Enter the index: 10 Enter the index value less than or equal to 2, because size of the array is 3 Normal flow...
https://sites.google.com/site/javaee4321/java/ExceptionDemo_UncheckedExcecption_AIOBEXE_App.zip?attredirects=0&d=1
Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/ExceptionDemo_UncheckedExcecption_AIOBEXE_App
Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/510730a975724a569c9343782aaa0bffd03472e7/BasicJava/ExceptionDemo_UncheckedExcecption_AIOBEXE_App/?at=master
See also:
No comments:
Post a Comment