1、JAVA异常体系结构图
2、进行异常捕获时,跟else if 结构相像,要先捕获小异常,再捕获大异常。
3、访问异常信息:
getMessage()返回该异常的详细描述字符串,printStackTrace()将该异常的跟踪栈信息输出到标准错误输出,printStackTrace(PrintStream s)将该异常的跟踪栈信息输出到指定输出流,getStackTrace()返回该异常的跟踪信息。
4、自动关闭资源的try语句:
try(声明、初始可关闭的资源){使用这些资源}/
5、throws
public class test
{
public static void main(String[] args)throws Exception
{
test();
}
private static void test() throws FileNotFoundException//必须比main抛出的异常小或者一样
{
FileInputStream fis = new FileInputStream("a.txt");
}
}
或者
public class test
{
public static void main(String[] args)throws Exception
{
test();
}
private static void test()
{
try
{
FileInputStream fis = new FileInputStream("a.txt");
} catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
}
6、catch和throw同时使用
catch(Exception e)
{
//异常的修复
//e.printStackTrace() 通过日志来记录异常发生的详细情况
//throw new 自定义异常 根据异常向应用使用者传达某种提示
}