本次PTA做业题集异常javascript
结合题集题目7-1回答java
使用try-catch处理异常。数据库
除了error和RuntimeException以外的异常都须要捕获。编程
题集题目7-2数组
try{ String input=sc.next(); arr[i]=Integer.parseInt(input); i++; }catch(Exception e){ System.out.println(e); }
在try程序块中放可能会出现异常的代码,当输入非整数的字符串时,会出现异常,则用catch捕获。
要处理异常可使程序更加健壮。安全
题集题目7-3
阅读Integer.parsetInt源代码app
这样能够不用绞尽脑汁去考虑各类错误,能够为处理某一类错误提供有效的解决方法,使编程效率提升。dom
public static double findMax(double[] arr,int begin, int end) throws IllegalArgumentException{ if(begin>=end) throw new IllegalArgumentException("begin:"+begin+">="+"end:"+end); else if(begin<0) throw new IllegalArgumentException("begin:"+begin+"<0"); else if(end>arr.length) throw new IllegalArgumentException("end:"+end+">"+"arr.length"); double max=arr[begin+1];//findMax方法用来返回arr数组中在下标begin(不包含begin)与end-1之间(包括end-1)的最大值。 for(int i=begin+1;i<end;i++){ if(max<arr[i]){ max=arr[i]; } } return max; }
当begin>=end,begin<0,end>arr.length时会抛出异常,且在异常中写明异常的缘由。让调用者明白为何产生异常,从而寻找方法。ide
题集题目6-3函数
好处:出错信息能够更详细,让用户更好发现错误。在本题中方法push(),若是栈满就抛出FullStackException,方法pop()和peek()要判断栈空,若是栈空就要抛出EmptyStackException,单纯返回错误值,信息太过简略,并且颇有可能错误代码也是正确的结果(好比-1),而抛出异常能够避免这些问题。
好处:代码中含有checked Exception,使用throws关键字抛出异常,能够不用辛辛苦苦地写try-catch子句。
题集题目6-1
子类异常必定要放在父类异常以前。
try块中后要跟多个catch块,catch块中的异常不得有继承关系。
byte[] content = null; FileInputStream fis = new FileInputStream("testfis.txt"); int bytesAvailabe = fis.available();//得到该文件可用的字节数 if(bytesAvailabe>0){ content = new byte[bytesAvailabe];//建立可容纳文件大小的数组 fis.read(content);//将文件内容读入数组 } System.out.println(Arrays.toString(content));//打印数组内容
注1:里面有多个方法都可能抛出异常。
功能2:须要添加finally关闭文件。不管上面的代码是否产生异常,总要提示关闭文件ing。若是关闭文件失败,提示关闭文件失败!
byte[] content = null; try{ FileInputStream fis = new FileInputStream("testfis.txt"); int bytesAvailabe = fis.available();//得到该文件可用的字节数 if(bytesAvailabe>0){ content = new byte[bytesAvailabe];//建立可容纳文件大小的数组 fis.read(content);//将文件内容读入数组 } }catch(FileNotFoundException | SizeDetermineFailed | MemoryAllocateFailed | ReadFailed e){ e.getStackTrace(); }finally{ if (fis != null) { try { //关闭资源也有可能出错 fis.close(); } catch (IOException e) { e.printStackTrace(); System.out.println("找不到文件xxx,请从新输入文件名"); }catch(Exception e){ System.out.println("打开或读取文件失败!"); } } } System.out.println(Arrays.toString(content));//打印数组内容
将关闭资源的操做放在finally块,由于finally块里的代码始终都要执行。有时候在关闭资源的时候也会遇到异常,这时候也要捕获。
byte[] content = null; try(FileInputStream fis = new FileInputStream("testfis.txt")){ int bytesAvailabe = fis.available();//得到该文件可用的字节数 if(bytesAvailabe>0){ content = new byte[bytesAvailabe];//建立可容纳文件大小的数组 fis.read(content);//将文件内容读入数组 } }catch(Exception e){ e.printStackTrace(); } System.out.println(Arrays.toString(content));
登陆lib.jmu.edu.cn,对图书进行搜索。而后登陆图书馆信息系统,查看个人图书馆。若是让你实现一个图书借阅系统,尝试使用面向对象建模。
集美大学的教师和学生,以及图书系统管理员。
暂时打算本身完成,算给本身一个挑战吧。。。
暂时打算分红三个主要模块解决,分别为用户登陆,用户管理模块,借阅管理模块,图书管理模块,和退出系统。系统须要添加图书、修改图书、删除图书、添加用户、修改用户、删除用户、借阅图书、归还图书和查阅图书等过程。为了安全和方便,打算用数据库来存储这些信息,能够采用表的形式来描述图书信息、解决信息、读者信息。
举1个例子说明你是如何使用异常处理机制让你的程序变得更健壮。
说明要包含2个部分:1. 问题说明(哪里会碰到异常)。2.解决方案(关键代码)
1.在登陆的时候,若是输入未存在的帐号,则会无响应。
2.抛出异常
关键代码: try{ if (Shopping.this.map.get(userInput.getText()).equals(s)) { new Menu().setVisible(true); Login.this.dispose(); } } catch(Exception e){ JOptionPane.showMessageDialog(null, "该帐号不存在,请从新输入!"); }
题目集:异常
在码云的项目中,依次选择“统计-Commits历史-设置时间段”, 而后搜索并截图
须要有两张图(1. 排名图。2.PTA提交列表图)
须要将每周的代码统计状况融合到一张表中。
周次 | 行数 | 新增行数 | 文件数 | 新增文件数 |
---|---|---|---|---|
1 | 91 | 91 | 5 | 5 |
2 | 504 | 413 | 18 | 13 |
3 | 1092 | 588 | 28 | 10 |
5 | 1158 | 129 | 34 | 6 |
6 | 1539 | 381 | 40 | 6 |
7 | 2023 | 484 | 49 | 9 |
8 | 2477 | 454 | 57 | 8 |
9 | 2709 | 232 | 63 | 6 |
10 | 3156 | 447 | 70 | 7 |
11 | 3531 | 375 | 79 | 9 |
课外练习
JavaTutorial中Questions and Exercises
练习总结
try { } finally { }
catch (Exception e) { }
What is wrong with using this type of exception handler?
try { } catch (Exception e) { } catch (ArithmeticException a) { }
int[] A;
A[0] = 0;
The JVM starts running your program, but the JVM can't find the Java platform classes. (The Java platform classes reside in classes.zip or rt.jar.)
A program is reading a stream and reaches the end of stream marker.
Before closing the stream and after reaching the end of stream marker, a program tries to read the stream again.
_b_error
_d_checked exception
_a_compile error
_c_no exception
public static void cat(File file) { RandomAccessFile input = null; String line = null; try { input = new RandomAccessFile(file, "r"); while ((line = input.readLine()) != null) { System.out.println(line); } return; } finally { if (input != null) { input.close(); } } }
修改以下: