Exception体系继承Throwable
常使用编译时异常,编译时出现异常则编译不能通过也不能运行,因此需要对编译时异常先进行处理处理编译时异常方法 try{}chatch(){}finally{}或者在方法名后进行抛出throws 异常名
FileInputStream fis = null;
try {
// 打开资源
fis = new FileInputStream("E:/a.txt");
// 读取文件中内容
System.out.println(fis.read());
} catch (Exception e) {
// e.printStackTrace();
System.out.println("找不到文件");
} finally {
// 关闭资源
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
自定义异常必须要继承Exception
public class UserException extends Exception{
public UserException() {
}
public UserException(String mgs) {
super(mgs);
}
}