My code wont execute | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
0

My code wont execute

Im having issues with writing this properly since the lesson on multiple exceptions did not teach me how to write other exceptions besides basic ones. 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 divide=num1/num2; } catch(ExceptionType1 e1) { throws ArithmeticException { if(num1==0||num2==0) { throw new ArithmeticException("Error: division by zero"); } } } catch(ExceptionType2 e2) { throws InputMismatchException { if(num1!=(int)num1||num2!=(int)num2) { throw new InputMismatchException("Error: wrong value type"); } } } } }

23rd Mar 2021, 4:23 PM
Serana Zentha
Serana Zentha - avatar
2 Respostas
+ 3
Serana Zentha There is no ExceptionType1 and ExceptionType2 exception. To catch multiple exception you can do like this:-- try { } catch(ArithmeticExcpetion e1) { System.out.print("Error: division by zero"); } catch (InputMismatchException e2) { System.out.print("Error: wrong value type"); } Here is complete solution: ----------- https://code.sololearn.com/cEt7aD6aCZYq/?ref=app
23rd Mar 2021, 5:25 PM
AĶ¢J
AĶ¢J - avatar
+ 5
Serana Zentha Anything which you want to throws just do after method not after catch So there should be int add() throws ArithmeticExcpetion, InputMismatchException { } https://www.sololearn.com/learning/2176/
23rd Mar 2021, 5:13 PM
AĶ¢J
AĶ¢J - avatar