您的位置:首页 > 其它

Exception(基础问题记录一下)

2018-03-17 22:40 393 查看
package com.test;

import java.io.IOException;

public class ExceptionTest {
public static void main(String[] args){
System.out.print("E");//这里因为是顺序执行,所以肯定会输出
try {
System.out.print("F");//这里因为是顺序执行,所以肯定会输出
badMethod();
System.out.print("A");//这里因为上一步抛出异常了,所以不可以执行
}catch (RuntimeException e){//NullPointerException
System.out.print("B");//这里因为try中捕获异常了,所以输出了
}finally {
System.out.print("C");
}
System.out.print("D");//这里因为是顺序执行,所以肯定会输出
}
public static void badMethod(){
throw new ClassCastException();
/*try {
//如果这里是运行时异常,而上面捕获的只是其中一个小的异常,那么就会抛出异常,反之可以
//如果这里是运行时异常,上面是非运行时异常,编译也不会通过
throw new IOException();//如果这里是非运行时异常,而主函数是运行时异常,也不会抛异常,因为非运行异常这里已经捕获了,而上面的运行时异常没发生所以没捕获。
} catch (IOException e) {
//e.printStackTrace();
System.out.print("G");
}*/
//System.out.print("G");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: