ImageVerifierCode 换一换
格式:PPTX , 页数:24 ,大小:109.83KB ,
资源ID:3329377      下载积分:20 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.docduoduo.com/d-3329377.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录   微博登录 

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(天津科技大学Java第六章 异常处理.pptx)为本站会员(dreamzhangning)主动上传,道客多多仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知道客多多(发送邮件至docduoduo@163.com或直接QQ联系客服),我们立即给予删除!

天津科技大学Java第六章 异常处理.pptx

1、1,第六章 异常处理, 异常(Exception)的概念异常处理方法自定义异常类,2,1 public class HelloWorld 2 public static void main(String args ) 3 int i=0; 4 String greetings = “Hello World!”,”Hello!”, 5 “HELLO WORLD!”; 6 while ( i4) 7 System.out.println(greetingsi); 8 i+; 9 10 11 ,Hello World! Hello! HELLO WORLD! Java.lang.ArrayIndex

2、OutOfBoundsExceptionat HelloWorld.main(HelloWorld.java:7),3,异常的概念,异常 在程序运行时打断正常程序流程的任何不正常情况。 异常的情况 试图打开的文件不存在 网络链接中断 操作符越界 要加载类文件不存在Java中定义了各种异常,4,Throwable,Error,Exception,VirtualMachineError,AWTError,RuntimeException,IOException,EOFException,FileNotFoundException,ArithmeticException,NullPointerExc

3、eption,IndexOutOfBoundsException,Java中定义的异常,5,Java处理的异常,Error 很难恢复的严重错误,一般不由程序处理。RuntimeException 程序设计或实现上的问题,如数组越界、除零、空引用等。其它异常 通常是由环境因素引起的,并且可以被处理的。 如文件不存在,无效URL等。,6,Java异常类体系中的常见异常,ArithmeticException: 除0错导致的异常 NullPointerException: 当对象没有实例化时,就试图通过该对象的变量访问其数据或方法。 ArrayIndexOutOfBoundsException: 数

4、组越界异常 IOException: 输入/输出时可能产生的各种异常 SecurityException: 由浏览器中负责安全控制的SecurityManager类抛出,7,6.2 异常处理,异常处理 指程序获得异常并处理,然后继续程序的执行。 为了使程序安全,Java要求如果程序中调用的方法有可能产生某种类型的异常,那么调用该方法的程序必须采取相应动作处理异常。处理异常的方式 方式一:捕获并处理异常 方式二:将方法中产生的异常抛出,8,捕获与处理异常,try 语句块 catch 语句块 finally 语句块,9,try语句块,一般形式:try Java statements /一条或多条可

5、能产生例外的java语句。try 语句后必须跟随至少一个catch或finally语句块。,10,catch语句块,catch语句块提供异常处理 一般格式: catch (SomeThrowableObject variableName) Java statements SomeThrowableObject:能够被处理的异常类名,必须是Throwable类的子类 variableName: 是异常处理程序中能够引用的代表被捕获异常变量名称。 Java statements: 当捕获到异常时执行的Java语句。,11,finally语句块,finally 语句块 无论是否发生异常都要执行。 一

6、般用于关闭文件或释放其他系统资源。,12,捕获与处理异常示例,public static void main(String args)int i = 0 ;String greetings=“Hello World!”,”Hello!”,”HELLO!”;while (i4)try System.out.println(greetingsi);catch(ArrayIndexOutOfBoundsException e)System.out.println(“Re-setting Index Value”);i=-1;finallySystem.out.println(“This is alw

7、ays printed”);i+; ,Hello World! This is always printed Hello! This is always printed HELLO! This is always printed Re-setting Index Value This is always printed,13,多种异常的同时处理,14,异常处理可以针对这个体系中的任意一个类。 叶结点 具体、专用的异常处理; 中间结点 通用的异常处理。可以处理该结点及其子类类型的异常。,例:writeList 方法: try . . . catch (Exception e) System.er

8、r.println(“Exception caught: “ + e.getMessage(); ,15,异常处理try ,catch和finally 语句,1 try 2 / code that might throw a partcular exception 3 catch(MyExceptionType e) 4 / code to excute if a MyExceptionType exception is thrown 5 catch (Exception e) 6 / code to execute if a general Exception exception is th

9、rown 7 finally,16,示例(P.165,6-2:ListOfNumbers.java,import java.io.*; import java.util.Vector; public class ListOfNumbers private ArrayList list; private static final int size = 10;public ListOfNumbers () list = new ArrayList(size);for (int i = 0; i size; i+)list.add(new Integer(i); public void writeL

10、ist() PrintWriter out = new PrintWriter(new FileWriter(“OutFile.txt“);for (int i = 0; i size; i+)out.println(“Value at: ” + i + “ = ” + list.get(i);out.close(); ,17,public void writeList() PrintWriter out = null;try System.out.println(“Entering try statement“);out = new PrintWriter( new FileWriter(“

11、OutFile.txt“);for (int i = 0; i size; i+)out.println(“Value at: “ + i + “ = “ +list.get(i); catch (ArrayIndexOutOfBoundsException e) System.err.println(“Caught ArrayIndexOutOfBoundsException: “ +e.getMessage(); catch (IOException e) System.err.println(“Caught IOException: “ + e.getMessage(); finally

12、 if (out != null) System.out.println(“Closing PrintWriter“);out.close(); else System.out.println(“PrintWriter not open“); ,18,writeList方法中的try语句块的执行可能有三种情况: 出现了IOException出现了数组越界错误正常退出,Entering try statementCaught IOException: OutFile.txtPrintWriter not open,Entering try statement Caught ArrayIndexO

13、utOfBoundsException: 10 = 10 Closing PrintWriter,Entering try statement Closing PrintWriter,19,异常处理抛出异常,可能产生异常的方法表明将不处理该异常,而该异常将被抛到调用该方法的程序。 例:public void troublesome( ) throws IOException 如果一个异常在返回到main()时还未被处理,则程序将非正常终止。,20,例: public Object pop() throws EmptyStackException Object obj;if (size = 0)

14、throw new EmptyStackException();obj = objectAt(size - 1);setObjectAt(size - 1, null);size-;return obj; 抛出异常的throw语句: throw someThrowableObject;,异常处理抛出异常,如何修改例子6-2?,21,22,6.3自定义异常类,定义异常类,是Exception类的子类,可包含普通类的内容。 public class ServerTimeOutException extends Exceptionprivate String reason;private int p

15、ort ;public ServerTimeOutException(String reason, int port)this.reason = reason;this.port = port;public String getReason()return reason;public int getPort()return port; ,23,抛出产生的异常,public void connectMe(String serverName) throwsServerTimeOutExceptionint success ;int portToConnect = 80;success = open

16、(serverName, portToConnect);if(success= -1)throw new ServerTimedOutException(“Could not connect”,80);,24,获得异常并处理,public void findServer()tryconnectMe(defaultServer);catch(ServerTimeOutException e)System.out.println(“Server timed out, try another”);tryconnectMe(alternateServer);catch(ServerTimeOutException e1)System.out.println(“No server avaliable”);,

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报