文档介绍:第13讲异常处理理解异常的概念 运用try块、catch块和finally块处理异常运用多重catch块处理异常运用嵌套try/catch块处理异常运用关键字throw和throws处理异常运用JAVA编写和使用自定义异常什么是异常(Exception)?lassExceptionRaised{publicExceptionRaised(){}publicintcalculate(intoperand1,intoperand2){intresult=operand1/operand2;returnresult;}publicstaticvoidmain(String[]args){ExceptionRaisedobj=newExceptionRaised();intresult=(9,0);(result);}}OS!异常情况异常程序突然终止并将控制交给操作系统在运行时发生的错误编译无问题,运行时出错,产生被0除的算数异常(系统自动产生),:在程序运行的过程中,Java捕获到的错误称之为异常Exception异常也是对象(异常类实例化),①JavaAPI定义的异常类(系统自动捕获到)②自定义异常类(系统不能自动捕获到,由用户自定义的类(extendsException及异常子类)Java异常类文件结束EOFException找不到文件FileNotFoundExceptionI/O异常的根类IOException数字转化格式异常,,如以零作除数ArithmeticException线程中断InterruptedException说明异常ExceptionArithmeticExceptionNullPointerExceptionObjectThrowableErrorThreadDeathSQLExceptionRuntimeExceptionNumberFormatException…自定义异常类异常类的层次结构Throwable具有两个子类,它们是Exception:处理用户程序应当捕获的异常情况Error:Error类的异常为内部低级别错误,因此在正常情况下不期望用户的程序捕获它们,由JVM抛出。AWTError………IFBISZEROGOTOERRORC=A/BPRINTCGOTOEXITERROR:处理异常的块“以零作除数,代码导致错误”DISPLAYEXIT:END处理异常2-1处理运行时错误的伪代码异常处理:在程序发生异常时,仍能以执行下去,而不是中断程序。手动引发异常指定由方法引发的异常tryfinallycatchthrowsthrow处理异常2-2要监控的程序语句包含在此块中以合理的方式捕获和处理异常释放资源等,不管有没有异常,都要执行try和catch块2-1trycatch异常执行catch后程序继续正常运行程序控制引发代码块单元基本语法:try{ //可能产生异常代码段 }catch(Exceptione){//捕获异常//异常处理}注意:①catch块一定要与try块一起,不能单独使用,位于try块之后;②一个try块可以有多个catch块try和catch块2-2演示:lassTestException{voiddisp(){ (〝hello〞);}publicstaticvoidmain(String[]args){TestExceptionobj=null;inta=1,b=0;(“Begin”);try{ ();intc=a/b;(c);}catch(NullPointerExceptionne){ ();}catch(ArithmeticExceptionne){(());}(“End”);}}Begin