0 like 0 dislike
8 views
asked by (4.5k points)

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

java divide by zero exception

Your answer

pre, code

Preview

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 (4.5k 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 8 views
0 like 0 dislike
1 answer 2.6k views
0 like 0 dislike
1 answer 5 views
0 like 0 dislike
1 answer 27 views
0 like 0 dislike
1 answer 21 views
0 like 0 dislike
1 answer 5 views
0 like 0 dislike
1 answer 35 views
0 like 0 dislike
1 answer 20 views
0 like 0 dislike
1 answer 13 views
...