异常中需要注意的问题有哪些?
//运行时异常继承了Exception
public class RuntimeException extends Exception {
static final long serialVersionUID = -7034897190745766939L;
/** Constructs a new runtime exception with {@code null} as its
* detail message. The cause is not initialized, and may subsequently be
* initialized by a call to {@link #initCause}.
*/
public RuntimeException() {
super();
}
......
// throws关键字用于在方法中显示此方法抛出了哪些异常
public static void main(String[] args) throws Exception {
//捕获异常
try {
//抛出异常
// throw关键字用于抛出异常
throw new RuntimeException();
} catch (RuntimeException e) {
//对异常进行处理
System.out.println("这是运行时异常");
} catch (Exception e){
System.out.println("捕获所有异常");
}
}
如何抛出异常?
【关键字】throw
—— 抛出异常
【作用】在特定的条件下抛出指定的异常
【关键字】throws
【作用】在【方法声明】位置,告知调用者,当前方法有哪些异常抛出;
【注意】