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.yiibai; 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...