0 like 0 dislike
20 views
asked by (5.7k points)

What I will receive exception if divide by zero in Java programming language?

java divide by zero exception

Your answer

Try to answer the question as detailed as possible.
Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.

1 Answer

0 like 0 dislike
answered by (5.7k points)

When dividing by zero you will receive ArithmeticException (java.lang.ArithmeticException) exception with message "/ by zero".

String exceptType = null;
String exceptMsg = null;
try {
		int a = 10 / 0;
} catch (ArithmeticException e) {
		exceptType = e.getClass().toString();
		exceptMsg = e.getMessage();
} catch (Exception e) {
		exceptType = e.getClass().toString();
		exceptMsg = e.getMessage();
} finally {
		System.out.println("Exception class/type: " + exceptType + ", exception message: " + exceptMsg);
}

Output:

Exception class/type: class java.lang.ArithmeticException, exception message: / by zero

Related questions

0 like 0 dislike
1 answer 10 views
0 like 0 dislike
1 answer 24.4k views
0 like 0 dislike
1 answer 7 views
0 like 0 dislike
1 answer 38 views
0 like 0 dislike
1 answer 25 views
0 like 0 dislike
1 answer 13 views
0 like 0 dislike
1 answer 44 views
0 like 0 dislike
1 answer 3 views
0 like 0 dislike
1 answer 29 views
...