异常处理

如何判断一个方法中可能抛出异常

  1. 该方法中出现throw语句
  2. 该方法调用了其他已经带throws子句的方法。

如果方法中可能抛出异常,有两种处理方法:

1.若当前方法有能力处理异常,则用Try-catch

  1. public void A(int x)
  2. {
  3. try{
  4. if (x == 0)
  5. throw new Exception("Zero");
  6. }
  7. catch(Exception e)
  8. {
  9. //do sth
  10. }
  11. }

2.若当前方法没有能力处理异常,则在方法的声明处写上throws语句。

  1. public void A(int x) throws Exception
  2. {
  3. if (x == 0)
  4. throw new Exception("Zero");
  5. }

finally:总是会被执行的语句。

不管try代码块中是否出现异常,finally代码块总是被执行。

(1)finally唯一不被执行的情况就是先执行了System.exit();

  1. public static void main(String[] args)
  2. {
  3. try
  4. {
  5. System.out.println("Begin");
  6. System.exit(0);
  7. }
  8. finally
  9. {
  10. System.out.println("Finally");
  11. }
  12. System.out.println("End");
  13. }

结果为Begin

  1. public static void main(String[] args) throws Exception
  2. {
  3. try
  4. {
  5. System.out.println("Begin");
  6. A(0); //抛出异常
  7. System.exit(0);
  8. }
  9. catch(Exception e)
  10. {
  11. System.out.println("Wrong");
  12. throw e;
  13. }
  14. finally
  15. {
  16. System.out.println("Finally");
  17. }
  18. System.out.println("End");
  19. }

输出结果为
Begin

Wrong

Finally

因为main方法已经抛出了异常,所以异常终止,不会继续执行下去。

假如注释掉throw e;这条语句

  1. public static void main(String[] args) throws Exception
  2. {
  3. try
  4. {
  5. System.out.println("Begin");
  6. A(0); //抛出异常
  7. System.exit(0);
  8. }
  9. catch(Exception e)
  10. {
  11. System.out.println("Wrong");
  12. //throw e;
  13. }
  14. finally
  15. {
  16. System.out.println("Finally");
  17. }
  18. System.out.println("End");
  19. }

输出结果为
Begin

Wrong

Finally

End

(2)假如含有return语句,那么finally会在return之前执行。

  1. public void A(int x) throws Exception
  2. {
  3. if (x == 0)
  4. throw new Exception("Zero"); //
  5. }
  6.  
  7. piblic int B(int x)
  8. {
  9. try
  10. {
  11. System.out.println("Begin"); //
  12. A(x); //
  13. return 1;
  14. }
  15. catch(Exception e)
  16. {
  17. System.out.println(e.getMessage()); //
  18. return 0; //
  19. }
  20. finally
  21. {
  22. System.out.println("Finally"); //
  23. }
  24. }
  25.  
  26. public static void main(String[] args)
  27. {
  28. System.out.println(B(0)); //
  29. }

输出结果为

Begin

Zero

Finally

0

throw与throws

注意throw与throws的区别:

throw是方法中抛出异常时使用的句子。

throws是方法名称后面用于声明该方法可能抛出的异常的句子。

异常处理的语法规则

  • try不能单独存在,后面至少要接一个catch或finally
  • try后面可以有0个或多个catch,可以有0个或1个finally,但不能2种都是0个。
  • try后面若有多个catch,则出现异常时会根据顺序去匹配catch,所以小类异常的catch应当放在更前一些。

数组

数组的声明与c++有所不同

int[] s;    (√)

int s[];    (√)

int s[30];    (×)

int[] s = new int[30];      (√)

int[] s = new int[]{1,2,3};  (√)

int[] s = new int[3]{1,2,3};  (×)

Java中数组的长度可以用s.length来获取。

线程初步

线程的创建和启动有2种方法:

1.继承Thread类

  1. public class S extends Thread
  2. {
  3. public void run()
  4. {
  5. //do sth;
  6. }
  7. }
  8.  
  9. S s = new S();
  10. s.start(); //start()会自动调用run()

2.实现Runnable接口

  1. public class S implements Runnable
  2. {
  3. public void run()
  4. {
  5.  
  6. }
  7. }
  8.  
  9. S s = new S();
  10. Thread thread = new Thread(s);
  11. thread.start();

尽管S实现了run方法,但S始终不是线程类,所以要重新创建一个线程类。

Thread中有这样的构造方法

Thread(Runnable runnable);

输入/输出

InputStream,OutputStream类 字节输入/输出流(最小的传输数据是字节)

Reader,Writer类 字符输入/输出流(最小的传输数据是字符)

字节输入流

所有的字节输入流都是InputStream类的子类。

文件输入流是InputStream的子类:

FileInputStream(File file);

FileInputStream(String name);

过滤输入流FilterInputStream(可以按照需要来读取相应的数据类型,如double,int)

FilterInputStream有以下2个子类:

BufferedInputStream  利用缓冲区来提高读写效率

DataInputStream    与DataOutputStream配合,可以从流中读取基本类型(int,double,char等)数据。

BufferedInputStream:

BufferedInputStream(InputStream in)  //装饰输入流In

BufferedInputStream(InputStream in, int size)  //装饰输入流in,指定缓冲区大小

  1. FileOutputStream out1 = new FileOutputStream("C:\\test.txt");
  2.  
  3. //装饰文件输出流
  4. BufferedOutputStream out2 = new BufferedOutputStream(out1);
  5.  
  6. //装饰带缓冲的文件输出流
  7. DataOutputStream out3 = new DataOutputStream(out2);
  8.  
  9. out3.writeByte(-12);
  10.  
  11. out3.flush(); //无论缓冲区是否满,都输出
  12.  
  13. out3.writeDouble(1.2);
  14.  
  15. out3.writeChar('a');
  16.  
  17. out3.close(); //close()会自动调用flush()
  18.  
  19. InputStream in1 = new InputStream("C:\\test.txt");
  20.  
  21. //装饰文件输入流
  22. BufferedInputStream in2 = new BufferedInputStream(in1);
  23.  
  24. //装饰带缓冲的文件输入流
  25. DataOutputStream in3 = new DataOutputStream(in2);
  26.  
  27. int a = in3.readByte();
  28.  
  29. double b = in3.readDouble();
  30.  
  31. char c = in3.readChar();
  32.  
  33. in3.close(0);

字节输出流同理

Reader类

Reader类有以下子类

InputStreamReader

FileReader

BufferedReader

InputStreamReader

InputStreamReader(InputStream in)  //按默认编码方式读取输入流中的字符

InputStreamReader(InputStream in, String charsetName)  //按给定编码方式读取输入流中的字符

FileReader(File file)

FileReader(String name)

BufferedReader

BufferedReader(Reader in)

BufferedReader(Reader in, int size)

  1. InputStream in1 = new FileInputStream("C:\\test.txt");
  2.  
  3. InputStreamReader in2 = new InputStreamReader(in1, "UTF-8");
  4.  
  5. BufferedReader in3 = new BufferedReader(in2);
  6.  
  7. StringWriter buffWriter = new StringWriter();
  8.  
  9. int data;
  10. while ((data = in3.read()) != -1)
  11. buffWriter.write(data);
  12.  
  13. in3.close;
  14.  
  15. String result = buffWriter.toString();

若有编码的问题,则应当考虑使用Reader/Writer而不是InpuStream/OutputStream.

Java逍遥游记读书笔记<三>的更多相关文章

  1. Java逍遥游记读书笔记<一>

    前言 必须先来一句,这是入门级别,高手勿喷~ 写Android的时候总有一些语句不是很理解,其实大部分是Java的内容,所以想系统的学下Java. 这本书——<Java逍遥游记>是在图书馆 ...

  2. Java逍遥游记读书笔记<二>

    Abstract抽象类 1.抽象类不能被实例化 2.抽象方法没有方法体 如: public abstract class Weapen { public abstract void attack(); ...

  3. java编程思想读书笔记三(11-21)

    十一:持有对象 >持有对象实例 ●数组将数字与对象联系起来.它保存类型明确的对象,查询对象时,不需要对结果做类型转换.他可以是多维的. 可以保存基本的数据类型.但是,数组一旦生成,容量就不会在变 ...

  4. 深入理解Java虚拟机之读书笔记三 内存分配策略

    一般的内存分配是指堆上的分配,但也可能经过JIT编译后被拆散为标量类型并间接地在栈上分配.对象主要分配在新生代的Eden区上,如果启动了本地线程分配缓冲,将按线程优先在TLAB上分配,少数情况下直接分 ...

  5. 《深入理解java虚拟机》读书笔记三——第四章

    第四章 虚拟机性能监控与故障处理工具 1.JDK命令行工具 jps命令: 作用:列出正在运行的虚拟机进程. 格式:jps [option] [hostid] 选项:-q 只输出LVMID(Local ...

  6. 《深入理解Java虚拟机》读书笔记三

    第四章 虚拟机性能监控与故障处理工具 1.JDK命令行工具 jps命令: 作用:列出正在运行的虚拟机进程. 格式:jps [option] [hostid] 选项:-q 只输出LVMID(Local ...

  7. JAVA编程思想读书笔记(三)--RTTI

    接上篇JAVA编程思想读书笔记(二) 第十一章 运行期类型判定 No1: 对于作为程序一部分的每个类,它们都有一个Class对象.换言之,每次写一个新类时,同时也会创建一个Class对象(更恰当的说, ...

  8. JAVA编程思想读书笔记(四)--对象的克隆

    接上篇JAVA编程思想读书笔记(三)--RTTI No1: 类的克隆 public class MyObject implements Cloneable { int i; public MyObje ...

  9. 《大型网站系统与Java中间件》读书笔记 (中)

    前言 只有光头才能变强. 文本已收录至我的GitHub仓库,欢迎Star:https://github.com/ZhongFuCheng3y/3y 回顾上一篇: <大型网站系统与Java中间件& ...

随机推荐

  1. 【转】maven常见问题问答

    转自:http://www.iteye.com/topic/973166 前言 Maven,发音是[`meivin],"专家"的意思.它是一个很好的项目管理工具,很早就进入了我的必 ...

  2. [Mybatis - 1A] - Cause: java.sql.SQLException: Column count doesn't match value count at row 1

    严重: Servlet.service() for servlet [springMVC] in context with path [/ExceptionManageSystem] threw ex ...

  3. View设置宽高

    public class ViewMeasuare extends View { public ViewMeasuare(Context context, AttributeSet attrs) { ...

  4. 获取当前View

    在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...

  5. JavaScript(三)-- DOM编程

    JavaScript编程中最基本的就是DOM编程,DOM是 Document Object Model文本对象模型,就是对DOM对象进行编程的过程. Java语言和Js都有针对于DOM的编程,两者类似 ...

  6. C# Conditional特性避免 预处理命令泛滥使用

    //#define CONDITION1 #define CONDITION2 using System; using System.Diagnostics; class Test { static ...

  7. Linux内核分析:实验八--Linux进程调度与切换

    刘畅 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 概述 这篇文章主要分析Li ...

  8. Github 入门基本操作

    翻译自:https://guides.github.com/activities/hello-world/ 文章概述: 什么是GitHub? 创建一个存储库 创建一个分支 做出承诺 打开拉请求 合并拉 ...

  9. 轻量级php框架phpk v1.0发布

    phpk框架简介 PHPK是一个简单易用,易于扩展的轻量级PHP框架.phpk不仅仅是一个php框架,也是一个js框架,内置一套全新的js内库,完全摒弃了庞大的jquery,所有的前端都是一个全新的微 ...

  10. 解决——CSS :before、:after ,当content使用中文时有时候会出现乱码

    问题: 在进行页面开发时,经常会使用:before, :after伪元素创建一些小tips,但是在:before或:after的content属性使用中文的话,会导致某些浏览器上出现乱码. 例如我遇到 ...