我是java新手,我不明白为什么异常类引用变量打印消息,而普通类的引用变量打印eclassname@jsjka为什么?
public class Exception11 {
int x,y;
public static void main(String[] args) {
try{
int total;
Exception11 e=new Exception11();
e.x=10;
e.y=0;
total=10/0;
System.out.println("Value of refernce variable: "+e);
System.out.println(total);
} catch (ArithmeticException h) {
System.out.println("number can not divide by the 0 Please try again");
int total;
Exception11 e=new Exception11();
System.out.println("Value of refernce variable: "+e);
System.out.println("Value of refernce variable: "+h);
}
}
}
回答-----------------------------------------
number can not divide by the 0 Please try again
Value of refernce variable: Exception11@4f1d0d
Value of refernce variable: java.lang.ArithmeticException: / by zero
最佳答案
您看到的是Object#toString
代表你的类(class)。相反,ArithmeticException 已经覆盖了此方法。您需要在Exception11
@Override
public String toString() {
return "Exception11 [x=" + x + ", y=" + y + "]";
}
关于java - 为什么异常类引用变量在java中打印消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19528502/