Question: I'm trying to teach myself about Exceptions in Java. I received an error in the code below. Could someone please explain to me why I
I'm trying to teach myself about Exceptions in Java. I received an error in the code below. Could someone please explain to me why I received the error as well as the solution? Thank you

package practice; import java.util.*;
public class Exception {
public static void main(String[] args) { Scanner sc = new Scanner(System.in); int x = 1; int y = 2; do{ try{ System.out.println("Enter first number: "); int num1 = sc.nextInt(); System.out.println("Enter second number: "); int num2 = sc.nextInt(); int sum = num1 + num2; System.out.print(sum); x=2; } catch(ArithmeticException e) { System.out.println("You can not do that. ERROR!"); } while(x == 1); } }
1 package practice; 2 import java util 4 public class Exception public static void main (Stringl] args) t Scanner sc new Scanner (System.in); int x 1; int y 2; 10 do 11 12 try System. out.println( "Enter first number 13 int num1 SC nextInt 14 15 System. out.println( "Enter second number 16 int num2. SC nextInt 17 18 int sum num1. num2. 19 System. out.print (sum); 20 22 catch (ArithmeticException e) t 23 System. out.println("You can not do that. ERROR 25 26 while (x 1); 27 28
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
