Try/catch body | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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); } }

23rd Mar 2019, 10:40 PM
Ben ogegbo
Ben ogegbo  - avatar
1 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
23rd Mar 2019, 10:57 PM
Denise Roßberg
Denise Roßberg - avatar