Try/catch body | Sololearn: Learn to code for FREE!

+1

Try/catch body

Can someone please go through the iteration so as to output n as 4 public class Program { public static void main(String[] args) { int n = 6; try { for(int i = 0; i < 3; i++) { n /= i; } } catch (Exception e) { n += 2; } System.out.print(n); } }

3/23/2019 10:40:50 PM

Ben ogegbo

1 Answer

New Answer

+8

n = 6; i = 0 --> n = 6/0 --> division by zero not allowed --> catch: n = 6+2 = 8 i = 1 --> n = 8/1 = 8 i = 2 --> n = 8/2 = 4 System.out.println(n) //output 4