异常
异常分类:
1. 编译时被检测的异常
a) 里面抛,外面声明,后面处理
classCalculation
{
int div(int a, int b)throws Exception//通过throws关键字声明该方法有可能出问题
{
return a/b;//如此处b为0
}
}
classExceptionDemo
{
public static void main(String[]args)//throws Exception
{
Calculation c = new Calculation();
try //try catch检测捕获异常,里面为可能出现问题的代码
{
int result = c.div(3,0);
System.out.println("result:"+result);
}
catch (Exception excep)
{
System.out.println(excep.toString());
}
System.out.println("continue");
}
}
其中,throws用于标识函数暴露出的异常。
toString()获取异常类名和异常信息,返回字符串
toMessage()获取异常信息,返回字符串
异常处理:
try
{
需要检测的代码;
}
catch(异常类变量)
{
异常处理代码;
}
finally
{
一定会执行的代码;
}
2. 编译时不被检测的异常(即运行时异常,RuntimeException及其子类)
RuntimeException是Exception中一个特殊的子类
如果在函数中抛出该异常,函数可以不用声明。如果在函数上声明了该异常,调用者可以不用进行处理。
之所以不用在函数声明,是因为不需要调用者处理
当该异常发生,希望程序停止,因为在运行时,出现了无法继续运行的情况,希望停止程序,对代码修正。此时就让自定义异常继承RuntimeException。
自定义异常
自定义异常(异常是一个类)必须继承(extends)Exception(异常)
1.当函数内部出现了throw抛出异常对象,就必须要给出对应的处理动作
方法:要么在内部 trycatch处理 要么在函数上声明让调用者处理
2.一般,函数内出现异常,函数上需要声明
如何定义异常?
由于父类中已经把异常信息的操作都完成了
子类只要在构造时,将异常信息传递给父类——通过super();
它就可以直接(应该是间接吧)通过getMessage方法获取自定义异常信息
throw和 throws的区别
1.throws 使用在函数上,throw使用在函数内
2.throws后面跟异常类,可以跟多个,用逗号隔开;throw后跟的是对象
程序示例:
class ExceptionDemo extendsRuntimeException//自定义异常
{
ExceptionDemo(Stringmsg)
{
super(msg);
}
}
class DivOperate //除法运算
{
publicint div(int a, int b)//throws RuntimeException
{
if(0>b){
thrownew ExceptionDemo("除数为负数");//容易丢掉 new
}
returna/b;
}
}
class RuntimeExceptionDemo
{
publicstatic void main(String[] args)
{
DivOperatediv_op = new DivOperate();
intresult = div_op.div(4,-1);
System.out.println("result"+result);
}
}
程序中自定义异常部分功能等同于:
privateString msg;
NegativeException(Stringmsg)
{
this.msg= msg;
}
publicString getMessage()
{
returnmsg;
}
异常处理还可以指明出问题的变量:
class NegativeException extends Exception//getMessage()
{
privateint num;
NegativeException(Stringmsg, int num)//Exception继承了Throwable
{
super(msg);
this.num= num;
}
publicint getNum()
{
returnnum;
}
}
class Demo
{
intdiv(int a, int b)throws NegativeException//此处用throws
{
if(0>b)
thrownew NegativeException("哈哈哈 我发现你的除数是负数啦",b);//通过throw关键字手动抛出自定义异常对象
returna/b;
}
}
class User_def_Exception
{
publicstatic void main(String[] args)
{
Demoobj = new Demo();
try
{
intresult = obj.div(6,-3);
System.out.println("result="+result);
}
catch(NegativeException neg_exce)
{
System.out.println(neg_exce.toString());
System.out.println("negativenumber: "+neg_exce.getNum());
}
System.out.println("ok");
}
}
finally
finally:一定会执行的代码。通常用于关闭资源。
catch是用于异常处理。
如果没有catch就代表异常没有被处理过,如果该异常是检测时异常,则必须声明。
异常在子父类覆盖中的体现:
如果子类方法发生了新的异常,就必须要进行try处理,绝对不能抛。
class NegativeException extends Exception
{
NegativeException(Stringmsg)
{
super(msg);
}
}
class Demo
{
publicint div(int a, int b)throws NegativeException
{
if(0>b)
throw new NegativeException("除数为负数");
returna/b;
}
}
class FinallyDemo
{
publicstatic void main(String[] args)
{
Demod = new Demo();
try
{
intresult = d.div(4,1);
System.out.println("result= "+result);
}
catch(NegativeException e)
{
System.out.println(e.toString());
return;
}
finally
{
System.out.println("hhdbq wan");
}
System.out.println("HelloWorld!");
}
}
一个经典的程序:
问题描述:张老师安排毕老师用电脑上课。
异常:
1.电脑蓝屏(可处理)
2.电脑冒烟(不可处理)
冒烟是电脑的异常,不是老师的异常,老师需要转换成自己的异常,向上级汇报
class Teacher
{
privateString name;
privateComputer cmpt;//需要先声明
Teacher(Stringname)//构造函数,初始化
{
this.name= name;
cmpt= new Computer();//即开始就有一台电脑
}
publicvoid teach() throws TeachStop//老师的行为
{
try
{
cmpt.run();
}
//有几个需要处理的异常就有几个catch()
catch(BlueScreen excp)//这个异常毕老师可以搞定
{
System.out.println(excp.getMessage());
cmpt.reset();
}
catch(Smokeexcp)//这个异常毕老师解决不了,抛出(给张老师)
{
System.out.println(excp.getMessage());
arrange();//安排同学先自学
thrownew TeachStop("我遇到问题:课堂无法继续");//此处要理解!!
}
System.out.println("***老师在教同学们学java***");
}
publicvoid arrange()
{
System.out.println("---->同学们先自学----");
}
}
class Computer
{
privateint flag = 3;
publicvoid run() throws BlueScreen,Smoke//可能出现的异常有几种抛几种
{
if(2==flag)
thrownew BlueScreen("???电脑蓝屏了!!!");
if(3==flag)
thrownew Smoke("???电脑冒烟了!!!");
System.out.println("@@@电脑运行起来了@@@");
}
publicvoid reset()
{
flag= 1;
System.out.println("***电脑重启中。。。***");
}
}
class BlueScreen extends Exception//定义蓝屏异常信息
{
//private String msg;因为继承了Exception
BlueScreen(Stringmsg)
{
super(msg);//父类都定义好了,直接用,爽!
}
}
class Smoke extends Exception//定义冒烟异常信息
{
Smoke(Stringmsg)
{
super(msg);
}
}
class TeachStop extends Exception//毕老师老师的异常
{
TeachStop(Stringmsg)
{
super(msg);
}
}
class ExceptionPractice
{
publicstatic void main(String[] args)
{
Teacherbxd = new Teacher("毕向东老师");
try
{
bxd.teach();
}
catch(TeachStop excp)
{
System.out.println(excp.getMessage());
System.out.println("张老师给你换电脑");
}
}
}