Throwable类的getMessage()方法 (Throwable Class getMessage() method)
getMessage() Method is available in java.lang package.
getMessage()方法在java.lang包中可用。
getMessage() Method is used to return the detailed descriptive message about the exception.
getMessage()方法用于返回有关异常的详细描述性消息。
getMessage() Method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
getMessage()方法是一个非静态方法,只能使用类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。
getMessage() Method does not throw an exception at the time of returning a detailed description of the message.
返回消息的详细描述时, getMessage()方法不会引发异常。
Syntax:
句法:
public String getMessage();
Parameter(s):
参数:
It does not accept any parameter.
它不接受任何参数。
Return value:
返回值:
The return type of the method is String, it returns descriptive message about the exception.
方法的返回类型为String ,它返回有关异常的描述性消息。
Example:
例:
// Java program to demonstrate the example
// of String getMessage() method of Throwable class
public class GetMessage {
public static void main(String args[]) throws Exception {
try {
div(-3, 0);
} catch (Exception ex) {
System.out.println("Get message :" + ex.getMessage());
}
}
// This method divide number by 0
public static void div(int d1, int d2) throws Exception {
int res = d1 / d2;
System.out.println("res :" + res);
}
}
Output
输出量
Get message :/ by zero
翻译自: https://www.includehelp.com/java/throwable-getmessage-method-with-example.aspx