by (9.4k points)
0 like 0 dislike
44 views

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

java divide by zero exception

1 Answer

0 like 0 dislike
by (9.4k 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

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.