抛出类getMessage()方法getMessage()方法在java.lang包中可用。
getMessage()方法用于返回有关异常的详细描述性消息。
getMessage()方法是一个非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。
返回消息的详细描述时,getMessage()方法不会引发异常。
语法:public String getMessage();
参数:它不接受任何参数。
返回值:
该方法的返回类型为String,它返回有关异常的描述性消息。
示例//Java程序演示示例
//getMessage()Throwable类的String方法的说明
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());
}
}
//此方法将数字除以0-
public static void div(int d1, int d2) throws Exception {
int res = d1 / d2;
System.out.println("res :" + res);
}
}
输出结果Get message :/ by zero