1.什么是异常
异常:中断了正常指令流的事件
2.异常的分类
uncheck exception(包括RuntimeException,以及RuntimeException它的子类),执行时出错
check exception:Exception的子类(除了RuntimeException)产生的异常,编译时报错
3.try..catch...finally结构的使用方法
4.throw的作用:抛出异常
5.throws的作用:在下面的例子中,说明setAge()这个方法有可能产生异常,但这个异常并不由setAge()这个方法来处理,而是由调用这个方法的地方来处理异常
(1).定义一个类User
class User
{
public int age;
public void setAge(int age) throws Exception
{
if(age<0)
{
Exception e=new Exception("年龄不能为负数");
throw e;
}
this.age=age;
}
}
(2).定义一个类Test(执行会抛异常,必须用try{}catch{}进行处理)
class Test
{
public void static main(String args[])
{
User user=new User();
user.setAge(-20);
}
}