JAVA 异常-finally

//仅作为学习笔记


/*

finally 代码块;定义一定执行的代码
通常用于关闭资源

public void method ()throws SQLException
{
	try
	{
		连接数据库;
		数据操作;
	}
	catch(SQLException e)
	{
		会对数据库进行异常处理;
	}
	finally
	{
		关闭数据库;
	}

}



*/



class FuShuException extends Exception
{

	FuShuException(String msg)
	{
		super(msg);
	}
}


class Demo 
{
	int div(int a,int b) throws FuShuException
	{
		if(b < 0)
		{
			throw new FuShuException("出现了除数是负数的情况!");
		}
		return a/b;
	}
}

class ExceptionDemo 
{
	public static void main(String[] args) 
	{

		Demo d = new Demo();


		try
		{
			int x = d.div(4,-1);							
			System.out.println(" x = "+ x);
		}
		catch (FuShuException e)
		{
			System.out.println(e.toString());
			return ;
		}
		finally
		{
			System.out.println("finally内的代码是一定会执行的!");
		}

		System.out.println("It is over!");


	}
}






格式一:
try{}
catch{}


格式二:
try{}
catch{}
finally{}


格式三:
try{}
finally{}


记住一点catch 是用来处理异常的,如果没有catch就代表异常没有被处理过,、
如果该异常时检测时异常,就必须进行声明.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值