public class Program { static int div(int a, int b) { if(b == 0) { throw ArithmeticException("Division by Zero"); } else { x = a/b; return x; } } public static void main(String[] args) { int x= div(42,2); System.out.println(x); } } what is wrong with this code .. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

public class Program { static int div(int a, int b) { if(b == 0) { throw ArithmeticException("Division by Zero"); } else { x = a/b; return x; } } public static void main(String[] args) { int x= div(42,2); System.out.println(x); } } what is wrong with this code ..

18th Sep 2016, 4:14 PM
subham singh
subham singh - avatar
5 Answers
+ 1
The below code is what you're suppose to write. throw new ArithmeticException("......"); You might also need to declare the throws keyword after the method signature if asked so.
18th Sep 2016, 4:28 PM
Ousmane Diaw
+ 1
do .... static int div(int a,int b) throws ArithmeticException
19th Sep 2016, 1:13 PM
bayram akdemir
bayram akdemir - avatar
0
use new to create anonymous object of the exception u wanna throw also u may always use try catch block if that won't work
19th Sep 2016, 12:42 AM
Ankur Sharma
Ankur Sharma - avatar
0
x is undefined inside div method
31st Oct 2016, 12:08 PM
DonBosco
0
syntax for throwing an exception is wrong...use 'throw new ArithmeticException("");' instead of throw ArithmeticException("");
31st Oct 2016, 12:12 PM
DonBosco