实验四--异常处理与多线程

实验四--异常处理与多线程

ID:35342351

大小:62.74 KB

页数:7页

时间:2019-03-23

实验四--异常处理与多线程_第1页
实验四--异常处理与多线程_第2页
实验四--异常处理与多线程_第3页
实验四--异常处理与多线程_第4页
实验四--异常处理与多线程_第5页
资源描述:

《实验四--异常处理与多线程》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库

1、实验四异常处理与多线程一、实验目的及要求目的:了解异常的概念,掌握异常处理的方法,掌握throws与throw关键字的区别与联系,掌握口定义异常的方法及用途。理解线程的概念,掌握Java的多线程机制,会用多线程编写Java程序。要求:(1)编写程序了解稈序中可能出现的运行时异常与非运行时异常(2)编写程序使用try-catch-finally处理异常,写程序掌握throws与throw的用法(3)理解线程的概念,会用Thread类、Runnable接口创建多线程,对两种实现多线程方式的方式进行对比二、实验环境计算机一台,JDK,

2、EditPlus三、实验原理异常处理方法,Java语言中多线程处理的原理四、实验内容与步骤(一)异常处理1.try-catch-finally处理异常过程importjava.io.*;publicclassEchoInput{publicstaticvoidmaiw(String[]args){System,out.printIn(Entertexttoecho");InputStreainReaderisr=newInputStreamReader(System,in):BufferedReaderinputReader=n

3、ewBufferedReader(isr);try{StringinputLine=inputReader.readLine();System.out.printIn(,vREAD:,v+inputLine):}catch(IOExceptionexc){System,out.printIn("Exceptionencountered:M+exc);}finally!System,out.printingMyjobhereisdone.,v):importjava.io.*;publicclassException4{publi

4、cstaticvoidsnaiziCStringargs[])throwsFileNotFoundException^IOException{FileinputStreamfis=newFileinputStreamCtext.txt'):intb;while((b=fis.read())!=-l){System,out.print(b);}fis.close():}}3.捕获异常(在Exceptionl的基础上加上捕获异常,观察程序运行结果)importjava.io.*;publicclassException5{publi

5、cstaticvoidmain(Stringargs[]){try{FileinputStreamfis=newFileinputStream('text.txt,Y):intb;while((b=fis.read())!=-l){System.out.print(b):}}catch(Fi1eNotFoundExceptione){System.out.println(e);System.out.printIn(message:1+e.getMessage()):e.printStackTrace(System.out);}c

6、atch(IOExceptione){System.out.printIn(e);4.自定义异常调用classMyotherExceptionextendsException{//用户定义的异常publicMyotherException(){publicMyotherException(Stringmsg){super(msg);publicclassExceptions

7、ExceptionfromF()f,);thrownewMyotherExceptionO;}publicstaticuoidg()throwsMyotherException{System.out.printInfThrowingMyotherExceptionFromg(),B);thrownewMyotherException(iBOriginateding()iB);}publicstaticuoidnain(String[]args){try{F();}catch(MyotherExceptione){e.prints

8、tackTrace();>try{g();}catch(MyotherExceptione){e.printstackTrace();(二)多线程处理1.继承Thread类实现多线程,试着修改setName方法的位置,看看运行效果//MyThread,java^

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。