本周主要进行第八章和第九章的学习。html
java中全部的错误都会打包为对象,能够try catch表明错误的对象后作一些处理。运用try、catch,还能够在捕捉处理错误以后,尝试恢复程序正常执行流程。java
import java.util.*; public class AVE { public static void main(String[] args) { Scanner console =new Scanner (System.in); double sum=0; int count =0; while(true) { try{ int number=console.nextInt(); if(number==0) { break; } sum+=number; count++; }catch (InputMismatchException ex){ System.out.printf("略过非整数输入:%s%n",console.next()); } } System.out.printf("AVE %.2f%n",sum/count); } }
程序运行结果:app
若是父类异常对象在子类异常前被捕捉,则catch子类异常对象的区块将永远不会被执行。误会被包装为对象,这些对象是可抛出的,所以设计错误对象都继承自java.lang.Throwable类。dom
在异常发生时,可以使用try 、catch处理当时可进行的异常处理,在catch区块进行完部分错误处理以后,可使用throw(注意不是throws)将异常再抛出。学习
import java.io.*; import java.util.Scanner; public class FileUtil { public static String readFile(String name) throws FileNotFoundException{ StringBuilder text = new StringBuilder(); try{ Scanner console = new Scanner (new FileInputStream(name)); while(console.hasNext()){ text.append(console.nextLine()) .append('\n'); } }catch(FileNotFoundException ex){ ex.printStackTrace(); throw ex; } return text.toString(); } }
只有java是采用受检异常的语言。ui
若收集的对象常常会有变更索引的状况,也许考虑连接方式操做的List会比较好,像是随时会有客户端登陆或注销的客户端List,使用LinkedList会有比较好的效率。设计
在收集过程当中如有相同对象,则再也不重复收集,若是有这类需求,可使用Set接口的操做对象。code
public class StackTraceDemo1 { public static void main(String[] args) { try { c(); } catch (NullPointerException ex) { ex.printStackTrace(); } } static void c() { b(); } static void b() { a(); } static String a() { String text = null; return text.toUpperCase(); } }
查看堆栈追踪最简单的方法,就是直接调用异常对象的 printStackTrace()。htm
在使用 throw 重抛异常时,异常的追踪堆栈起点,还是异常的发生根源,而不是重抛异常的地方,对象
public class StackTraceDemo2 { public static void main(String[] args) { try{ c(); }catch(NullPointerException ex){ ex.printStackTrace(); } } static void c(){ try{ b(); }catch(NullPointerException ex) { ex.printStackTrace(); throw ex; } } static void b(){ a(); } static String a(){ String text = null; return text.toUpperCase(); } }
不管try区块中有无异常,若撰写finally区块,则finally区块必定会被执行
public class FinallyDemo { public static void main(String[] args) { System.out.println(test(true)); } static int test(boolean flag){ try{ if(flag){ return 1; } }finally{ System.out.println("finally..."); } }return 0; }
收集对象的共同行为定义在Collection中。
java.util.ArrayDeque操做了Deque接口,可使用ArrayDeque来操做容量有限的堆栈。
若是对象有操做Queue,并打算以队列方式使用,且队列长度受限,一般建议使用offer()、poll()与peek()等方法。
import java.util.*; interface Request{ void execute(); } public class RequestQueue { public static void main(String[] args) { Queue requests = new LinkedList(); offerRequestTo(requests); process(requests); } static void offerRequestTo(Queue requests){ for(int i=1;i<6;i++){ Request request = new Request(){ public void execute(){ System.out.printf("处理数据%f%n",Math.random()); } }; requests.offer(request); } } static void process(Queue requests){ while(requests.peek()!=null){ Request rquest = (Request) request.poll(); request.execute(); } } }
学习的东西愈来愈难了,光敲代码是没有用的,仍是要多思考,多练习,不然会学得很不扎实,好比上周考试的操做题就不会。因此之后还须要在java上投入更多的时间练习。
代码托管截图:
代码行数(新增/累积) | 博客量(新增/累积) | 学习时间(新增/累积) | 重要成长 | |
---|---|---|---|---|
目标 | 5000行 | 30篇 | 400小时 | |
第一周 | 100/100 | 2/2 | 10/10 | 初步认识java |
第二周 | 150/250 | 1/3 | 12/22 | 掌握Java基础语法 |
第三周 | 537/787 | 2/4 | 20/42 | 认识对象,对象封装 |
第四周 | 500/1287 | 1/5 | 20/62 | 继承与多态,接口与多态 |
第五周 | 300/1587 | 1/6 | 20/82 |