我的JAVA程序;
class MyException extends Exception
{
private int detail ; MyException(int a)
{
detail = a ;
} public String toString()
{
return "MyException["+detail+"]" ;
}
}public class ExceptionDemo
{
static void compute(int a) throws MyException
{
System.out.println("Called compute" + a + ".") ;
if( a > 10 )
throw new MyException( a ) ;
System.out.println( "Normal exit" ) ;
} public static void main(String args[])
{
try
{
compute( 1 ) ;
compute( 11 ) ;
}
catch( MyException e )
{
System.out.println( "Exception caught "+ e ) ;
}
}
}输出;
Called compute1.
Normal exit
Called compute11.
Exception caught MyException[11]
请问是谁调用了public String toString()函数啊?
class MyException extends Exception
{
private int detail ; MyException(int a)
{
detail = a ;
} public String toString()
{
return "MyException["+detail+"]" ;
}
}public class ExceptionDemo
{
static void compute(int a) throws MyException
{
System.out.println("Called compute" + a + ".") ;
if( a > 10 )
throw new MyException( a ) ;
System.out.println( "Normal exit" ) ;
} public static void main(String args[])
{
try
{
compute( 1 ) ;
compute( 11 ) ;
}
catch( MyException e )
{
System.out.println( "Exception caught "+ e ) ;
}
}
}输出;
Called compute1.
Normal exit
Called compute11.
Exception caught MyException[11]
请问是谁调用了public String toString()函数啊?
这一句。
由于和String 进行'+'运算,e被转成了String(通过自动调用toString)
学习ing
还是算了吧
自己多理解理解
ioriogami,谢谢你
这是我首次发帖
谢谢你的热情答复
分就给你了