JAVA | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

JAVA

Hello world! I am a new Java learner and have a doubt regarding one of pro challenge about multiple exception (try/catch); my code works and output the correct answer as per test 2 verification but it tells me that is not a valid answer. I would like just to understand why, for more details here below the details and the code. Thanks a lot for your help! Multiple Exceptions You need to write a divider program which will operate with integers, The program you are given should take two integers as input and execute the division, but we need to handle two exceptions: 1. the divider shouldn't be zero 2. both inputs should be integers. Complete the program to handle them. For the first exception, the program should output "Еrror: division by zero"; and for the second one, "Error: wrong value type". Sample Input 1 b Sample Output Error: wrong value type Use ArithmeticException for first exception and InputMismatchException for the second one. Code: import java.util.Scanner; import java.util.InputMismatchException; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); try { int num1 = scanner.nextInt(); int num2 = scanner.nextInt(); /* 1. Еrror: division by zero 2. Error: wrong value type */ //your code goes here int num3 = num1/num2; System.out.println(num3); } catch(ArithmeticException e1) { System.out.print("Error: division by zero"); } catch(InputMismatchException e2){ System.out.println("Error: wrong value type"); } } }

19th Jan 2021, 1:54 PM
Atok
Atok - avatar
8 Answers
+ 7
//Please Subscribe to My Youtube Channel //Channel Name: Fazal Tuts4U import java.util.Scanner; import java.util.InputMismatchException; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); try { int num1 = scanner.nextInt(); int num2 = scanner.nextInt(); System.out.println(num1/num2); } catch(ArithmeticException e) { System.out.println("Еrror: division by zero"); } catch(InputMismatchException e){ System.out.println("Error: wrong value type"); } } }
4th Sep 2021, 4:18 PM
Fazal Haroon
Fazal Haroon - avatar
+ 7
import java.util.Scanner; import java.util.InputMismatchException; public class MultipleExceptions { public static void main(String[] args) { try { Scanner scanner = new Scanner(System.in); //System.out.println("Enter two numbers "); int num1 = scanner.nextInt(); int num2 = scanner.nextInt(); System.out.println(num1/num2); } catch(ArithmeticException e) { System.out.println("Error: division by zero"); } catch(Exception e){ System.out.println("Error: wrong value type"); } } }
28th Jul 2022, 2:18 AM
Ariaie M
Ariaie M - avatar
+ 2
darucko Yes it's weird because the string written in another language like Russian. Some characters looks like same in many languages but their ASCII value maybe different.
19th Jan 2021, 2:51 PM
A͢J
A͢J - avatar
+ 2
import java.util.Scanner; import java.util.InputMismatchException; public class MultipleExceptions { public static void main(String[] args) { try { Scanner scanner = new Scanner(System.in); //System.out.println("Enter two numbers "); int num1 = scanner.nextInt(); int num2 = scanner.nextInt(); System.out.println(num1/num2); } catch(ArithmeticException e) { System.out.println("Error: division by zero"); } catch(Exception e){ System.out.println("Error: wrong value type"); } } }
13th Nov 2022, 8:56 AM
Pooja Patel
Pooja Patel - avatar
+ 2
import java.util.Scanner; import java.util.InputMismatchException; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); try { int num1 = scanner.nextInt(); int num2 = scanner.nextInt(); System.out.println(num1/num2); } catch(ArithmeticException ex) { System.out.print("Mistake: division by zero"); } catch(InputMismatchException ex){ System.out.println("Mistake: wrong value type"); } } }
12th Jan 2023, 8:20 PM
Sachin Chavan
Sachin Chavan - avatar
0
OK, it is weird but once I made copy/paste the String "Error: division by zero" from the previous comment on the exercise it finally worked!!
19th Jan 2021, 2:10 PM
Atok
Atok - avatar
0
import java.util.Scanner; import java.util.InputMismatchException; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); try { int num1 = scanner.nextInt(); int num2 = scanner.nextInt(); /* 1. Еrror: division by zero 2. Error: wrong value type */ //your code goes here int num3 = num1/num2; System.out.println(num3); } catch(ArithmeticException e1) { System.out.print("Еrror: division by zero"); } catch(InputMismatchException e2){ System.out.println("Error: wrong value type"); } } }
31st Mar 2021, 3:02 PM
Prãbîñ Pãñtã
Prãbîñ Pãñtã - avatar
0
import java.lang.*; class Main { public static void main(String[ ] args) { Name name = new Name(); //set priority name.setPriority(5); Welcome welcome = new Welcome(); //set priority welcome.setPriority(5); welcome.start(); name.start(); } } //extend the Thread class class Welcome extends Thread{ public void run() { System.out.println("Welcome!"); } } //extend the Thread class class Name extends Thread{ public void run() { System.out.println("Please enter your name"); } }
12th Jan 2023, 8:23 PM
Sachin Chavan
Sachin Chavan - avatar