Handling Exceptions in Loops
This program demonstrates handling exceptions inside loops.
Problem Statement
Write a Java program that handles exceptions during loop iterations.
Source Code
| 1 | public class LoopExceptionExample { |
| 2 | public static void main(String[] args) { |
| 3 | System.out.println("Eduinq Loop Exception Example"); |
| 4 | int[] arr={1,0,2}; |
| 5 | for(int i=0;i<arr.length;i++){ |
| 6 | try { |
| 7 | int result=10/arr[i]; |
| 8 | System.out.println("Eduinq Result: "+result); |
| 9 | } catch(Exception e) { |
| 10 | System.out.println("Eduinq Exception at index "+i+": "+e.getMessage()); |
| 11 | } |
| 12 | } |
| 13 | } |
| 14 | } |
Program Output
Eduinq Loop Exception Example Eduinq Result: 10 Eduinq Exception at index 1: / by zero Eduinq Result: 5
Explanation
Exceptions inside loops must be handled per iteration. - Prevents loop termination due to single error. - Improves robustness and reliability. - In enterprise systems, loop exception handling ensures continuous processing. Conclusion: Shows handling exceptions inside loops.