简体   繁体   English

用户定义的异常中如何调用toString?

[英]How toString is called in userdefined Exception?

Here how the toString() method executes no explicit call is made to it? 在这里,toString()方法如何不执行对它的显式调用? can anyone please explain the control flow of it 谁能解释一下它的控制流程

class MyException extends Exception
{
    String str1;
    MyException(String str2) {
       str1=str2;
    }
    public String toString(){ 
       return ("Output String = "+str1) ;
    }
}

class CustomException{
    public static void main(String args[]){

       try{   throw new MyException("Custom");   }
       catch(MyException e){ System.out.println(e); }
    }
}

When an object is passed to System.out.println it's toString() method is called (so this result can be displayed), whatever this object might be (Exceptions are also objects). 当一个对象被传递给System.out.println时,无论该对象可能是什么(异常也是对象),都会调用toString()方法(这样就可以显示此结果)。

So, when you do: 因此,当您这样做时:

catch(MyException exp) { 
    System.out.println(e);  
}

System.out.println takes e , call's it's toString() and displays this result onto console. System.out.println接受e ,将其称为toString()并将此结果显示在控制台上。

Since toString() is part of Object , which all objects inherit from, System.out.println(Object o) ( out is a PrintStream ) knows there is a toString() implementation and it uses it ( String.valueOf(Object o) is called first). 由于toString()Object一部分,所有对象都从System.out.println(Object o)继承,所以System.out.println(Object o)outPrintStream知道存在toString()实现,并且使用它( String.valueOf(Object o)首先被称为)。

Since toString has been overridden - it uses the specified implementation. 由于toString已被覆盖-它使用指定的实现。

Prints an Object and then terminate the line. 打印一个对象,然后终止该行。 This method calls at first String.valueOf(x) to get the printed object's string value, then behaves as though it invokes print(String) and then println(). 此方法首先调用String.valueOf(x)以获取打印对象的字符串值,然后像先调用print(String)然后再调用println()一样操作。

The documentation of the PrintWriter.println(Object) method says: PrintWriter.println(Object)方法文档说:

Prints an Object and then terminates the line. 打印一个对象,然后终止该行。 This method calls at first String.valueOf(x) to get the printed object's string value, then behaves as though it invokes print(String) and then println() . 此方法首先调用String.valueOf(x)来获取打印对象的字符串值,然后像先调用print(String)然后再调用println()

The method String.valueOf(Object) uses the parameter's toString() method unless it is null. 除非它为null,否则方法String.valueOf(Object)使用参数的toString()方法。

This is why toString() is called. 这就是为什么调用toString()原因。

System.out.println prints an Object and then terminate the line. System.out.println打印一个对象,然后终止该行。

This method calls at first String.valueOf(x) to get the printed object's string value, then behaves as though it invokes print(String) and then println() . 此方法首先调用String.valueOf(x)来获取打印对象的字符串值,然后像先调用print(String)然后再调用println()

String.valueOf(x) return if the argument is null, then a string equal to "null"; String.valueOf(x)如果参数为null,则返回等于“ null”的字符串;否则返回false。 otherwise, the value of obj.toString() is returned, in your case, it's 否则,返回obj.toString()的值,在您的情况下,它是

Output String = Custom 输出字符串=自定义

When you call System.out.println(e) : 当您调用System.out.println(e)

     public void println(Object x) {
         String s = String.valueOf(x);
         synchronized (this) {
             print(s);
             newLine();
         }
     }

This method calls String.valueOf(x) : 此方法调用String.valueOf(x)

public static String valueOf(Object obj) {
    return (obj == null) ? "null" : obj.toString();
}

Do you see obj.toString() ? 您看到obj.toString()吗? You can reference source-code here PrintStream.println , String.valueOf 您可以在此处引用源代码PrintStream.printlnString.valueOf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM