探索JAVA的异常

张开发
2026/5/31 9:49:26 15 分钟阅读
探索JAVA的异常
探索JAVA的异常一.异常是什么?1.1Error1.2Checked1.3RuntimeExceptionArithmeticExceptionArrayIndexOutBoundsExceptionNullPointerException二.异常五个关键字2.1.异常的抛出2.2.异常的申明2.3异常的捕获三.自定义异常一.异常是什么?在日常的生活中,我们或多或少的都会生病,例如感冒发烧,我们需要吃药,然后就吃好了,而在我们JAVA,也会出现错误,这就是异常,学习异常能让我们避免大多数错误;异常的大父类是:throwable;下面是图画:1.1Error这里的Error就是错误的意思,他意味着严重,不可逆的错误,常见于内存耗尽,资源泄露这些;一旦发生,那就无力回天,代码重新再构思;1.2CheckedChecked是受查异常,简单来说就是在编译器自动识别出来的错误,在运行之前就出现的异常;出来运行时异常(RuntimeExceotion)以及子类,其他的是受查异常;就像这种我随便,写个错误,他就会标记出错误,这在日常些代码是常见的;1.3RuntimeException这个也叫受查异常,就是编译器没看出来,你也没看出来,运行就报出异常错误;当然受查异常也不只这一个;下面列举常见的一些运行时异常:ArithmeticException算书异常publicclassboke_1{publicstaticvoidmain(String[]args){System.out.println(10/0);}}ArrayIndexOutBoundsException数组越界异常publicstaticvoidmain(String[]args){int[]arraynewint[]{1,2,3};array[3]5;}NullPointerException空指针异常publicstaticvoidmain(String[]args){System.out.println((char[])null);}当然还有很多;二.异常五个关键字try,catch,finally,throw,throws这五个都是重要的2.1.异常的抛出在平时,如果我们发现异常,需要及时的抛出异常,来中止程序,以防更大的错误,例如程序会崩溃;publicstaticintgetArrayNum(int[]array,intindex){if(arraynull){thrownewNullPointerException(空指针异常);}elseif(index0||indexarray.length){thrownewArrayIndexOutOfBoundsException(数组越界异常);}returnarray[index];}publicstaticvoidmain(String[]args){int[]arraynewint[]{1,2,3};intresgetArrayNum(array,3);}注意:异常的抛出必须在方法中;2.2.异常的申明这个属于处理异常的一种方法,另一种是捕获异常;4异常申明俗称老赖行为,表达的意思就是:我知道这个异常,但我不处理,把他抛给别人来处理,只要异常没传到JVM就可以了,但注意异常并没有解决:publicclassStudentimplementsCloneable{publicStringname;publicintage;protectedObjectclone()throwsCloneNotSupportedException{returnsuper.clone();}}StudentpublicclassTestDemo1{publicstaticvoidmain(String[]args)throwsCloneNotSupportedException{Studentstu1newStudent();Studentstu2(Student)stu1.clone();}}如果不加的话会报错:简单说就是这个异常发生了,但我们没有处理,而是通过申明的方式告诉可能解决可能不解决;本质异常并没有解决;2.3异常的捕获这是异常处理的另一种方法:格式try{代码块}catch(异常类型 异常变量){}.....finally{}下面用捕获的方法来真正解决上面的克隆代码publicstaticvoidmain(String[]args){Studentstu1newStudent();try{Studentstu2(Student)stu1.clone();}catch(CloneNotSupportedExceptione){System.out.println(捕获了这个异常);}catch(Exceptione){}}可以看到并没报错:注意:如果在Student类中实现了这个代码:捕获里面的代码就不会打印;e.printStackTrace();这是把异常出现的栈空间位置表明出来;观察一下这个代码是不是少了个finally,少了会错吗?不会;finally的作用是释放空间,现在体现不出了,之后大项目,必须要释放空间;不管咋样,如果写了finally,就一定会执行;publicstaticintFunc(){inti;try{return10;}finally{return20;}}publicstaticvoidmain(String[]args){intresFunc();System.out.println(res);}打印的结果是什么?最开始return语句分两步:一,存储返回值,二返回返回值;第一步结束后会执行finally语句;最后会返回20;publicstaticintFunc(){inti;try{return10;}finally{System.out.println(会执行finally语句);return20;}}三.自定义异常JAVA中最核心的就是类,我们现在学的异常就是一种类,在我们日常生活中,会遇到自己写出来的代码出现各种错误,但JAVA给的异常不可能全部覆盖,这就需要我们手动去定义异常类;下面写一个登录的代码,并定义异常;publicclassLogin{privateStringnameniu ge;privateintage15;publicvoidLoginfo(Stringname,intage){try{if(!this.name.equals(name)){thrownewUserException(用户名错误);}if(this.age!age){thrownewAgeException(年龄错误);}}catch(UserExceptione){System.out.println(捕获了这个异常);e.printStackTrace();}catch(AgeExceptione){System.out.println(捕获了这个异常);e.printStackTrace();}}可以看到AgeException,UserException是JAVA中所没有的,是我们自己要处理的业务异常;来自定义异常:AgeExceptionpublicclassAgeExceptionextendsException{publicAgeException(){}publicAgeException(Stringmes){super(mes);}}UserExceptionpublicclassUserExceptionextendsException{publicUserException(){}publicUserException(Stringmessage){super(message);}}

更多文章