java -异常


4.10 异常
    4.10.1 异常的体系
异常:是在运行时期发生的不正常情况。
在java中用类的形式对不正常情况进行了描述和封装对象。描述不正常的情况的类,就称为异常类。
1.以前正常流程代码和问题处理代码相结合,现在将正常流程代码和问题处理代码分离,提高阅读性。
2.其实异常就是java通过面向对象的思想将问题封装成了对象,用异常类对其进行描述。
3.不同的问题用不同的类进行具体的描述。比如角标越界、空指针异常等等。
4.问题很多,意味着描述的类也很多,将其共性进行向上抽取,形成了异常体系。
不正常情况分成了两大类:
Throwable:无论是error,还是异常、问题,问题发生就应该可以抛出,让调用者知道并处理。
该体系的特点就在于Throwable及其所有的子类都具有可抛性。
可抛性到底指的是什么呢?怎么体现可抛性呢?
其实是通过两个关键字来体现的:throwsthrow,凡是可以被这两个关键字所操作的类和对象都具备可抛
性。
1.一般不可处理的:Error
特点:是由jvm抛出的严重性问题。
这种问题发生,一般不针对性处理,直接修改程序。
2.可以处理的:Exception
该体系的特点:
子类的后缀名都是用其父类名作为后缀,阅读性很强。
Throwable中的方法:
1.getMessage():获取异常信息,返回字符串。
2.toString():获取异常类名和异常信息,返回字符串。
3.printStackTrace():获取异常类名和异常信息,以及异常出现在程序中的位置,返回值void。
4.printStackTrace(PrintStreams):通常用该方法将异常内容保存在日志文件中,以便查阅。
    示例:
01.  class Demo{
02.         public static int method(int[] arr, int index){
03.               if(arr == null){
04.                     throw new NullPointerException("数组的引用不能为空!");
05.               }
06.               if(index >= arr.length ){
07.                     throw new ArrayIndexOutOfBoundsException("数组的角标越界:" + 
index);
08.               }
09.               return arr[index];
10.         }
11.  }
12.  class ExceptionDemo{
13.         public static void main(String[] args){
14.              int[] arr = new int[3];
15.              Demo.method(arr,30);
16.         }
17.  }
复制代码
运行结果:
4.10.2 自定义异常
可以自定义出的问题称为自定义异常。
对于角标为负数的情况,可以用负数角标异常来表示,负数角标这种异常在java中并没有定义过。
那就按照java异常的创建思想,面向对象,将负数角标进行自定义描述,并封装成对象。
这种自定义的问题描述称为自定义异常。
P.S.
如果让一个类成为异常类,必须要继承异常体系,因为只有成为异常体系的子类才有资格具备可抛性,才可
以被两个关键字所操作:throws、throw。
自定义类继承Exception或者其子类,通过构造函数定义异常信息。
    示例:
01.  ClassDemoExceptionextendsException
02.  {
03.  DemoException(Stringmessage)
04.  {
05.  super(message);
06.  }
07.  }
复制代码

通过throw将自定义异常抛出。
throws和throw的区别:
1.throws用于标识函数暴露出的异常类,并且可以抛出多个,用逗号分隔。throw用于抛出异常对象。
2.thorws用在函数上,后面跟异常类名。throw用在函数内,后面跟异常对象。
定义功能方法时,需要把出现的问题暴露出来让调用者去处理,那么就通过throws在函数上标识。
在功能方法内部出现某种情况,程序不能继续运行,需要进行跳转时,就用throw把异常对象抛出。
    示例:
01.  classFuShuIndexExceptionextendsException{
02.  FuShuIndexException(){}
03. 
04.  FuShuIndexException(Stringmsg){
05.  super(msg);
06.  }
07.  }
08. 
09.  classDemo{
10.  publicstaticintmethod(int[]arr,intindex)throwsFuShuIndexException{
11.  if(index<0){
12.  thrownewFuShuIndexException("数组的角标是负数啦!");
13.  }
14.  returnarr[index];
15.  }
16.  }
17. 
18.  classExceptionDemo{
19.  publicstaticvoidmain(String[]args)throwsFuShuIndexException{
20.  int[]arr=newint[3];
21.  Demo.method(arr,-30);
22.  }
23.  }
复制代码
运行结果:
异常的分类:
1.编译时被检测异常:只要是Exception和其子类都是,除了特殊子类RuntimeException体系。
这种问题一旦出现,希望在编译时就进行检测,让这种问题有对应的处理方式。
这样的问题都可以针对性的处理。
2.编译时不检测异常(运行时异常):就是Exception中的RuntimeException和其子类。
这种问题的发生,无法让功能继续,运算无法运行,更多是因为调用的原因导致的或者引发了内部状态的
改变导致的。
那么这种问题一般不处理,直接编译通过,在运行时,让调用者调用时的程序强制停止,让调用者对代码
进行调整。
所以自定义异常时,要么继承Exception,要么继承RuntimeException。
    示例:
01.  classFuShuIndexExceptionextendsRuntimeException{
02.  FuShuIndexException(){}
03. 
04.  FuShuIndexException(Stringmsg){
05.  super(msg);
06.  }
07.  }
08. 
09.  classDemo{
10.  publicstaticintmethod(int[]arr,intindex){//RuntimeException没有必要用throws抛出,并
不是必须要处理
11.  if(index<0){
12.  thrownewFuShuIndexException("数组的角标是负数啦!");
13.  }
14.  returnarr[index];
15.  }
16.  }
复制代码
运行结果:
P.S.
RuntimeException是那些可能在Java虚拟机正常运行期间抛出的异常的超类。
可能在执行方法期间抛出但未被捕获的RuntimeException的任何子类都无需在throws子句中进行声明。
异常处理的捕捉形式:
可以对异常进行针对性处理的方式。
具体格式是:
try{
//需要被检测异常的代码。
}
catch(异常类变量)//该变量用于接收发生的异常对象
{
//处理异常的代码。
}
finally{
//一定会执行的代码;
}
P.S.
finally代码块只有一种情况不会被执行,就是在之前执行了System.exit(0)。
处理过程:
try中检测到异常会将异常对象传递给catch,catch捕获到异常进行处理。
finally里通常用来关闭资源。比如:数据库资源,IO资源等。
需要注意:try是一个独立的代码块,在其中定义的变量只在该变量块中有效。
如果在try以外继续使用,需要在try外建立引用,在try中对其进行初始化。IO,Socket就会遇到。
    示例:
01.  class FuShuIndexException extends RuntimeException{
02.        FuShuIndexException(){}
03. 
04.        FuShuIndexException(String msg){
05.               super(msg);
06.        }
07.  }
08. 
09.  class Demo{
10.         public static int method(int[] arr, int index) throws 
NullPointerException,FuShuIndexException{
11.               if(arr == null)
12.                     throw new NullPointerException("没有任何数组实体");
13.               if(index < 0){
14.                     throw new FuShuIndexException("数组的角标是负数啦!");
15.              }
16.               return arr[index];
17.        }
18.  }
19. 
20.  class ExceptionDemo{
21.         public static void main(String[] args){
22.              int[] arr = new int[3];
23.              try{
24.                    int num = Demo.method(arr,-30);
25.                    System.out.println("num:" + num);
26.              } catch(NullPointerException e){
27.                    System.out.println(e);
28.              } catch(FuShuIndexException e){
29.                    System. out.println("message:" + e.getMessage());
30.                    System.out.println("string:" + e);
31.                    e.printStackTrace(); //jvm 默认的异常处理机制就是调用异常对象的这个方
法。
32.                    System.out.println("负数角标异常!!!");
33.              } catch(Exception e){//Exception的catch放在最下面,先处理有针对性的异常
34.                    System.out.println(e);
35.              }
36.              System.out.println("over" );
37.        }
38.  }
复制代码
运行结果:

异常处理的原则:
1.函数内容如果抛出需要检测的异常,那么函数上必须要声明。
否则,必须在函数内用try/catch捕捉,否则编译失败。
2.如果调用到了声明异常的函数,要么try/catch,要么throws,否则编译失败。
3.什么时候catch,什么时候throws呢?
功能内容可以解决,用catch。
解决不了,用throws告诉调用者,由调用者解决。
4.一个功能如果抛出了多个异常,那么调用时,必须有对应多个catch进行针对性处理。
内部有几个需要检测的异常,就抛几个异常,抛出几个,就catch几个。
    示例:
01.  classDemo{
02.  publicintshow(intindex)throwsArrayIndexOutOfBoundsException{
03.  if(index<0)
04.  thrownewArrayIndexOutOfBoundsException("越界啦!");
05.  int[]arr=newint[3];
06.  returnarr[index];
07.  }
08.  }
09. 
10.  classExceptionDemo{
11.  publicstaticvoidmain(String[]args){
12.  Demod=newDemo();
13.  try{
14.  intnum=d.show(-3);
15.  System.out.println("num="+num);
16.  }catch(ArrayIndexOutOfBoundsExceptione){
17.  System.out.println(e.toString());
18.  System.exit(0);//退出jvm
19.  }finally{//通常用于关闭(释放)资源
20.  System.out.println("finally");//由于前面执行了System.exit(0);,故不会执行此语句。
21.  }
22.  System.out.println("over");
23.  }
24.  }
复制代码
运行结果:
trycatchfinally代码块组合特点:
1.trycatchfinally
2.trycatch(多个):当没有资源需要释放时,可以不用定义finally。
3.tryfinally:异常无法直接catch处理,但是资源必须关闭。
    示例:
01.  voidshow()throwsException{
02.  try{
03.  //开启资源
04.  thrownewException();
05.  }finally{
06.  //关闭资源
07.  }
08.  }
复制代码
    异常综合案例:
01.  /*
02.  毕老师用电脑上课。
03. 
04.  问题领域中涉及两个对象。
05.  毕老师,电脑。
06. 
07.  分析其中的问题。
08.  比如电脑蓝屏,冒烟等。
09.  */
10.  classLanPingExceptionextendsException{
11.  LanPingException(Stringmsg){
12.  super(msg);
13.  }
14.  }
15. 
16.  classMaoYanExceptionextendsException{
17.  MaoYanException(Stringmsg){
18.  super(msg);
19.  }
20.  }
21. 
22.  classNoPlanExceptionextendsException{
23.  NoPlanException(Stringmsg){
24.  super(msg);
25.  }
26.  }
27. 
28.  classComputer{
29.  privateintstate=1;//02
30.  publicvoidrun()throwsLanPingException,MaoYanException{
31.  if(state==1)
32.  thrownewLanPingException("电脑蓝屏啦!");
33.  if(state==2)
34.  thrownewMaoYanException("电脑冒烟啦!");
35.  System.out.println("电脑运行");
36.  }37.  publicvoidreset(){
38.  state=0;
39.  System.out.println("电脑重启");
40.  }
41.  }
42. 
43.  classTeacher{
44.  privateStringname;
45.  privateComputercomp;
46. 
47.  Teacher(Stringname){
48.  this.name=name;
49.  comp=newComputer();
50.  }
51. 
52.  publicvoidprelect()throwsNoPlanException{
53.  try{
54.  comp.run();
55.  System.out.println(name+"讲课");
56.  }catch(LanPingExceptione){
57.  System.out.println(e.toString());
58.  comp.reset();
59.  prelect();
60.  }catch(MaoYanExceptione){
61.  System.out.println(e.toString());
62.  test();
63.  //可以对电脑进行维修
64.  thrownewNoPlanException("课时进度无法完成,原因:"+e.getMessage());
65.  }
66.  }
67.  publicvoidtest(){
68.  System.out.println("大家练习");
69.  }
70.  }
71. 
72.  classExceptionDemo{
73.  publicstaticvoidmain(String[]args){
74.  Teachert=newTeacher("毕老师");
75.  try{
76.  t.prelect();
77.  }catch(NoPlanExceptione){
78.  System.out.println(e.toString()+".......");
79.  System.out.println("换人");
80.  }
81.  }
82.  }
复制代码
运行结果:
异常的注意事项:
1.RuntimeException以及其子类如果在函数中被throw抛出,可以不用在函数上声明。
2.子类在覆盖父类方法时,父类的方法如果抛出了异常,那么子类的方法只能抛出父类的异常或者该异常的
子类。
3.如果父类抛出多个异常,那么子类只能抛出父类异常的子集。
简单说:子类覆盖父类只能抛出父类的异常或者子类的子集。
P.S.
如果父类的方法没有抛出异常,那么子类覆盖时绝对不能抛,就只能try。
4.11 Object类
Object:所有类的根类。
Object是不断抽取而来,具备着所有对象都具备的共性内容。
    示例:
01.  classPerson{
02.  privateintage;
03.  Person(intage){
04.  this.age=age;
05.  }
06.  }
07. 
08.  classDemo{
09.  }
10. 
11.  classObjectDemo{
12.  publicstaticvoidmain(String[]args){
13.  Personp1=newPerson(20);
14.  Personp2=newPerson(20);
15.  Personp3=p1;
16.  
17.  Demod=newDemo();
18. 
19.  System.out.println(p1==p2);//false
20.  System.out.println(p1.equals(p2));//false
21.  System.out.println(p1.equals(p3));//true
22.  System.out.println(p1.equals(d));//false
23.  }
24.  }
复制代码
运行结果:
P.S.
==以及Object类的equals方法默认都是根据对象的哈希值判断两个对象是否相等。
可以通过覆盖Object的equals方法来重写比较规则。
    示例:
01.  class Person{
02.         private int age ;
03.         Person( int age){
04.               this.age = age;
05.         }
06.         //比较Person的年龄,是否是同龄人
07.         //一般都会覆盖此方法,根据对象的特有内容,建立判断对象是否相同的依据。
08.         public boolean equals(Object obj){
09.               if(!(obj instanceof Person))
10.                     throw new ClassCastException("类型错误");
11.               Person p = (Person)obj;
12.               return this .age == p.age;
13.        }
14.  }
15. 
16.  class ObjectDemo{
17.         public static void main(String[] args){
18.              Person p1 = new Person(20);
19.              Person p2 = new Person(20);
20. 
21.              System. out.println(p1.equals(p2));
22.        }
23.  }
复制代码
运行结果:
Object类的toString方法默认返回的内容是“对象所属的类名+@+对象的哈希值(十六进制)”。
    示例:
01.  classPerson{
02.  privateintage;
03.  Person(intage){
04.  this.age=age;
05.  }
06.  publicinthashCode(){
07.  returnage;
08.  }
09.  }
10. 
11.  classObjectDemo{
12.  publicstaticvoidmain(String[]args){
13.  Personp1=newPerson(20);
14. 
15.  System.out.println(p1);
16.  System.out.println(p1.getClass().getName()+"$"+
Integer.toHexString(p1.hashCode()));
17.  }
18.  }


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值