When you use any object in java System.out.println(object);
It will always call java object.toString()
method.
Throwable class is overriding the toString() method to display information for all exceptions. As all exceptions are sub classes for throwable class, exceptions will be displayed in className:message format.
Internal implementation of toString() method in Throwable class.
public String toString() {
String s = getClass().getName();
String message = getLocalizedMessage();
return (message != null) ? (s + ": " + message) : s;
}
In your case ArithmeticException extends RuntimeException, RuntimeException extends Exception and Exception extends Throwable. As ArithmeticException, RuntimeException and Exception classes are not overriding toString() method. That's why Throwable toString() method is executed.
If Throwable also not override the toString() then it will execute the toString() of java.lang.Object which has default implementation like below
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}