异常处理

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 自定义异常 根据异常向应用使用者传达某种提示

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值