05-05throwable类的方法

···
package com.lesson06;

/*throwable类的方法 三种
1.String toString
2.String getMessage
3.void printStackTrace 一般用第三种,JVM也默认用这个,会显示出错的行号
*/

public class ExceptionDemo {

public static void main(String[] args) {
    // TODO Auto-generated method stub

/*
try {
function();
} catch (Exception e) {
System.out.println(e.toString()); // toString方法 有返回值
}
/
/

try {
function();
} catch (Exception e) {
System.out.println(e.getMessage()); // getMessage方法 有返回值
}
*/
try {
function();
} catch (Exception e) {
e.printStackTrace();// printStackTrace方法 无返回值
}
}
public static void function() throws Exception{
throw new Exception("异常了");
}
}
java.lang.Exception: 异常了
at com.lesson06.ExceptionDemo.function(ExceptionDemo.java:34)
at com.lesson06.ExceptionDemo.main(ExceptionDemo.java:28)

你可能感兴趣的:(05-05throwable类的方法)