当前位置:高等教育资讯网  >  中国高校课件下载中心  >  大学文库  >  浏览文档

北京大学计算机系:《Java 2实用教程》(第3版)PPT教学课件 第6章 异常处理

资源类别:文库,文档格式:PPT,文档页数:27,文件大小:413.5KB,团购合买
一、理解异常和异常处理 二、区别异常的类型:Error与Exception,必检异常与免检异常 三、在方法头中声明异常 四、在方法中抛出异常 五、用try-catch块处理异常 六、了解异常的传播 七、在try-catch块中重新抛出异常 八、在try-catch块中使用finally子句 九、了解何时使用异常 十、声明自定义异常类
点击下载完整版文档(PPT)

第6章异常处理 理解异常和异常处理 区别异常的类型: Error与 Exception,必检异常与免检异常 在方法头中声明异常 在方法中抛出异常 o/用try- catch块处理异常 了解异常的传播 o在try- catch块中重新抛出异常 o在try- catch块中使用 finally.子句 o了解何时使用异常 o声明自定义异常类

 理解异常和异常处理  区别异常的类型:Error与Exception,必检异常与免检异常  在方法头中声明异常  在方法中抛出异常  用try-catch块处理异常  了解异常的传播  在try-catch块中重新抛出异常  在try-catch块中使用finally子句  了解何时使用异常  声明自定义异常类 第6章 异常处理

程序错误:语法错误、运行错误、逻辑错误 浯法错误是指代码没有遵循语言的规则,可以由编 器检查发现。 o在程序运行过程中,如果环境发现一个不可能执行 的操作,就会出现运行错误。 o如果程序没有按照预期的方案执行,就会发生逻辑 错误

程序错误:语法错误、运行错误、逻辑错误 语法错误是指代码没有遵循语言的规则,可以由编 译器检查发现。 在程序运行过程中,如果环境发现一个不可能执行 的操作,就会出现运行错误。 如果程序没有按照预期的方案执行,就会发生逻辑 错误

Runtime errors import javax. swing. JOptionPane public class Test i public static void main(string[] args)i String input JOptionPane showInputDialog(null, nPlease enter an integer )i nt number Integer parseInt (input)i 如果这行出现异常,就会跳过 // Display the result 该方法中其余的行,程序终止 JOptionPane showMessageDialog(null, u The number entered is "+ number)i System. exit(o)i V程序结束

Runtime Errors import javax.swing.JOptionPane; public class Test { public static void main(String[] args) { String input = JOptionPane.showInputDialog(null, "Please enter an integer"); int number = Integer.parseInt(input); // Display the result JOptionPane.showMessageDialog(null, "The number entered is " + number); System.exit(0); } } 如果这行出现异常,就会跳过 该方法 中其余的行,程序终止 程序结束

程序 readfile(自然语言描述) readFile open the file determine its size allocate the much memory read the file into memory close the file

程序readFile(自然语言描述) readFile{ open the file; determine its size; allocate the much memory; read the file into memory; close the file; }

传统错误处理:通过函数返回值 readFile initialize errorcode=0i open the filei the Fileopen) determine its size if(getTheFileLenght t allocate the much memory; if(getEnough Memory read the file into memory: (readFailed) errorCode=-1 } felse t error Code=-2: felse t errorCode=-3: close the file if(the FildDidntclose & errorCode==ort errorcode 4 felse t errorCode else t errorCode=-6: return errorCode

传统错误处理:通过函数返回值 readFile{ initialize errorCode=0; open the file; if(theFilesOpen){ determine its size; if(getTheFileLenght){ allocate the much memory; if(getEnoughMemory){ read the file into memory; if(readFailed){ errorCode=-1; } }else { errorCode=-2; } }else { errorCode=-3; } close the file; if(theFildDidntClose && errorCode==0){ errorCode=-4; }else { errorCode=-5; } } else { errorCode=-6; } return errorCode; }

异常处理机制 readFile open the file determine its size allocate the much memory read the file into memory close the file 3 catch(fileOpen Failed)t handl 1 catch (sizedetermination Failed)tt handle 3 catch(memory Allocation Failed)t handle: 3 catch (readFailed) handle 3 catch(fileCloseFailed)t handle

异常处理机制 readFile{ try{ open the file; determine its size; allocate the much memory; read the file into memory; close the file; } catch(fileOpenFailed){ handle; } catch(sizedeterminationFailed){ handle; } catch(memoryAllocationFailed){ handle; } catch(readFailed){ handle; } catch(fileCloseFailed){ handle; } }

Catch runtime errors import javax. swing. JOptionPane public class Test t public static void main(string[] args)i String input JOptionPane showInputDialog(null, "Please enter an integer") 如果这行出现异常,就会跳过try子句中其余 int number Integer parseInt(input) 的行,并把程序控制转移到 catch子句 // Display the result JOptionPane showMessageDialog(null ii The number entered is number catch( Excepti。nex) JOptionPane. showMes sageDialog(null iNcorrect input: an integer is required")i 捕获并处理这个异常后,程序控制转移到 Iry- catch块之后的第一条语句 System. out. println ("Execution continues ...")i System. exit(o)

Catch Runtime Errors import javax.swing.JOptionPane; public class Test { public static void main(String[] args) { try { String input = JOptionPane.showInputDialog(null, "Please enter an integer"); int number = Integer.parseInt(input); // Display the result JOptionPane.showMessageDialog(null, "The number entered is " + number); } catch (Exception ex) { JOptionPane.showMessageDialog(null, "Incorrect input: an integer is required"); } System.out.println("Execution continues ..."); System.exit(0); } } 如果这行出现异常,就会跳过try子句中其余 的行,并把程序控制转移到catch子句 捕获并处理这个异常后,程序控制转移到 Try-catch块之后的第一条语句

EXception Classes ClassNot Found Exception IOException ArithmeticException AWTException NullPointer Except RuntimeExe IndexOutOfBounds Exception Throuble Several more classes galArgumentException Linkage error Several more classes VirtualMachine Error Erro AWTError

Exception Classes LinkageError Error AWTError AWTException Throwable ClassNotFoundException VirtualMachineError IOException Exception RuntimeException Object ArithmeticException NullPointerException IndexOutOfBoundsException Several more classes Several more classes Several more classes IllegalArgumentException

System Errors ClassNot Found Exception IOExcepti Arithmetic Exception Exception AWTException lullPointer Excepti RuntimeException IndexOutofBounds Exception Object Throwal Several more classes IllegalArgument Exception System errors是由Java虚 Several more classes 拟机抛出并在Eror类中描 LinkageError 述的。Eror类描述内部的 VirtualMachine Error 系统错误。这种错误很少 Error 发生,一旦发生,除了通 AWTErro 知用户以及尽量稳妥地结 束程序外,几乎什么也不 Several more classes 能做

System Errors LinkageError Error AWTError AWTException Throwable ClassNotFoundException VirtualMachineError IOException Exception RuntimeException Object ArithmeticException NullPointerException IndexOutOfBoundsException Several more classes Several more classes Several more classes IllegalArgumentException System errors是由Java虚 拟机抛出并在Error类中描 述的。Error类描述内部的 系统错误。这种错误很少 发生,一旦发生,除了通 知用户以及尽量稳妥地结 束程序外,几乎什么也不 能做

Exceptions Exceptions类描述由程序 ClassNot Found Exception 和外部环境引起的错误 这些错误能通过程序 IOException 捕获和处理。 Arithmetic Exception ception AWTException INullPointer Except RuntimeException IndexOutOfBounds Exception hrowab Several more classes IllegalArgument Exception LinkageError Several more classes Virtualmachine erro Error AWTError Several more classes

Exceptions LinkageError Error AWTError AWTException Throwable ClassNotFoundException VirtualMachineError IOException Exception RuntimeException Object ArithmeticException NullPointerException IndexOutOfBoundsException Several more classes Several more classes Several more classes IllegalArgumentException Exceptions类描述由程序 和外部环境引起的错误 ,这些错误能通过程序 捕获和处理

点击下载完整版文档(PPT)VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
共27页,试读已结束,阅读完整版请下载
相关文档

关于我们|帮助中心|下载说明|相关软件|意见反馈|联系我们

Copyright © 2008-现在 cucdc.com 高等教育资讯网 版权所有