A Simple Java Exception Example!
SLVIKI
9:16 AM
0
This is a simple example that look for and catch an exception. Here In this code I've used ArrayIndexOutOfBoundsException as It will generate an exception if we try to get access of an index out of the array boundary.
Here is the code that used in this program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | // Demostrate exception handling. class ExcDemo1 { public static void main(String args[]) { int nums[] = new int[4]; try { System.out.println("Before exception is generated."); // Generated an index out-of-bounds exception. nums[4] = 10; System.out.println(nums[3]); } catch (ArrayIndexOutOfBoundsException exc) { // catch the exception System.out.println("Index out-of-bounds!"); } System.out.println("After catch statement."); } } |
And here is the output which you get from the above code..
Before exception is generated.
Index out-of-bounds!
After catch statement.
If you have any questions, feel free to ask!
Enjoy!
No comments