java语言异常的使用方法和处理经验

来源:互联网 发布:php软件开发工具 编辑:程序博客网 时间:2024/05/18 00:44
异常处理的捕捉形式:
这是可以对异常进行针对性处理的方式。

具体格式是:
try
{
//需要被检测异常的代码。
}
catch(异常类 变量)//该变量用于接收发生的异常对象
{
//处理异常的代码。
}
finally
{
//一定会被执行的代码。
}

异常处理的原则:
1,函数内容如果抛出需要检测的异常,那么函数上必须要声明。
否则必须在函数内用trycatch捕捉,否则编译失败。

2,如果调用到了声明异常的函数,要么trycatch要么throws,否则编译失败。

3,什么时候catch,什么时候throws 呢?
功能内容可以解决,用catch。
解决不了,用throws告诉调用者,由调用者解决 。

4,一个功能如果抛出了多个异常,那么调用时,必须有对应多个catch进行针对性的处理。

内部又几个需要检测的异常,就抛几个异常,抛出几个,就catch几个。


class FuShuIndexException extends Exception{FuShuIndexException(){}FuShuIndexException(String msg){super(msg);}}class Demo{public int method(int[] arr,int index)//throws NullPointerException,FuShuIndexException{if(arr==null)throw new NullPointerException("没有任何数组实体");if(index<0)throw new FuShuIndexException();return arr[index];}}class  ExceptionDemo4{public static void main(String[] args) {int[] arr = new int[3];Demo d = new Demo();try{int num = d.method(null,-1);System.out.println("num="+num);}catch(NullPointerException e){System.out.println(e.toString());}catch (FuShuIndexException e){System.out.println("message:"+e.getMessage());System.out.println("string:"+e.toString());e.printStackTrace();//jvm默认的异常处理机制就是调用异常对象的这个方法。System.out.println("负数角标异常!!!!");}/*catch(Exception e)//多catch父类的catch放在最下面。{}*/System.out.println("over");}}

异常的注意事项:

1,子类在覆盖父类方法时,父类的方法如果抛出了异常,
那么子类的方法只能抛出父类的异常或者该异常的子类。

2,如果父类抛出多个异常,那么子类只能抛出父类异常的子集。

简单说:子类覆盖父类只能抛出父类的异常或者子类或者子集。 

注意:如果父类的方法没有抛出异常,那么子类覆盖时绝对不能抛,就只能try .


interface  Inter{void function();}class D implements Inter{public void function()//throws Exception{}}class A extends Exception {}class B extends A{}class C extends Exception {}Exception|--A|--B|--Cclass Fu{void show()throws A{}}class Test{void method(Fu f)//Fu f  = new Zi();{try{f.show();}catch (A  a){}}}class Zi extends Fu{void show()throws C{}}class  {public static void main(String[] args) {Test t = new Test();t.show(new Zi());}}

下面是一个现实中异常例子:

/*毕老师用电脑上课。问题领域中涉及两个对象。毕老师,电脑。分析其中的问题。比如电脑蓝屏啦。冒烟啦。*/class LanPingException extends Exception{LanPingException(String msg){super(msg);}}class MaoYanException extends Exception{MaoYanException(String msg){super(msg);}}class NoPlanException extends Exception{NoPlanException(String msg){super(msg);}}class Computer{private int state = 2;public void run()throws LanPingException,MaoYanException{if(state==1)throw new LanPingException("电脑蓝屏啦!!");if(state==2)throw new MaoYanException("电脑冒烟啦!!");System.out.println("电脑运行");}public void reset(){state = 0;System.out.println("电脑重启");}}class Teacher{private String name;private Computer comp;Teacher(String name){this.name = name;comp = new Computer();}public void prelect()throws NoPlanException{try{comp.run();System.out.println(name+"讲课");}catch (LanPingException e){System.out.println(e.toString());comp.reset();prelect();}catch (MaoYanException e){System.out.println(e.toString());test();//可以对电脑进行维修。//throw e;throw new NoPlanException("课时进度无法完成,原因:"+e.getMessage());}}public void test(){System.out.println("大家练习");}}class Demo {public static void main(String[] args) {Teacher t  = new Teacher("毕老师");try{t.prelect();}catch (NoPlanException e){System.out.println(e.toString()+"......");System.out.println("换人");}}}/*class NoAddException extends Exception{}void addData(Data d)throws NoAddException{连接数据库try{添加数据。出现异常 SQLException();}catch(SQLException e){//处理代码。throw new NoAddException();}fianlly{关闭数据库。}}*/



1 0
原创粉丝点击