java中如何通过捕获异常来读取子类异常消息
如何通过使用exception捕获getStudent方法引发的异常来打印该异常。要打印异常消息,请使用同名的getMessage方法。此代码将失败。我把它留给OP来确定它失败的地方和原因。你的代码甚至不会编译。看看函数定义。它缺少int-id,用于打印消息,System.out.printlne.getMessage;您是否阅读了官方的JavaDoc以了解例外情况?java中如何通过捕获异常来读取子类异常消息,java,exception,Java,Exception,如何通过使用exception捕获getStudent方法引发的异常来打印该异常。要打印异常消息,请使用同名的getMessage方法。此代码将失败。我把它留给OP来确定它失败的地方和原因。你的代码甚至不会编译。看看函数定义。它缺少int-id,用于打印消息,System.out.printlne.getMessage;您是否阅读了官方的JavaDoc以了解例外情况? class Student { public static Student getStudent(int id) thr
class Student {
public static Student getStudent(int id) throws StudentNotFoundException {
throw new StudentNotFoundException("Student not found");
}
public static void main(String... args){
try{
Student student = getStudent(1);
}catch(Exception e){
//want to print the exception message passed from getUser() method i.e. Student not found
}
}
}
e.printStackTrace(); // print to error message and stack trace to standard error
System.out.println(e.getMessage()); // print only the message to standard output