Java.lang.Throwable.getMessage() 方法
描述
java.lang.Throwable.getMessage() 方法返回此 throwable 的详细消息字符串。
声明
以下是 java.lang.Throwable.getMessage() 方法的声明。
public String getMessage()
参数
NA
返回值
此方法返回此 Throwable 实例的详细消息字符串(可以为 null)。
异常
NA
示例
下面的例子展示了 java.lang.Throwable.getMessage() 方法的使用。
package com.tutorialspoint;
import java.lang.*;
public class ThrowableDemo {
public static void main(String[] args) throws Throwable {
try {
newException();
} catch(Throwable e) {
System.err.println(e);
// returns the detail message string of this Throwable instance
System.out.println(e.getMessage());
}
}
public static void newException() throws Exception {
System.out.println("This is newException() function");
throw new Exception("new Exception...");
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
This is newException() function new Exception... java.lang.Exception: new Exception...

