Java逍遥游记读书笔记<三>
异常处理
如何判断一个方法中可能抛出异常
- 该方法中出现throw语句
- 该方法调用了其他已经带throws子句的方法。
如果方法中可能抛出异常,有两种处理方法:
1.若当前方法有能力处理异常,则用Try-catch
public void A(int x)
{
try{
if (x == 0)
throw new Exception("Zero");
}
catch(Exception e)
{
//do sth
}
}
2.若当前方法没有能力处理异常,则在方法的声明处写上throws语句。
public void A(int x) throws Exception
{
if (x == 0)
throw new Exception("Zero");
}
finally:总是会被执行的语句。
不管try代码块中是否出现异常,finally代码块总是被执行。
(1)finally唯一不被执行的情况就是先执行了System.exit();
public static void main(String[] args)
{
try
{
System.out.println("Begin");
System.exit(0);
}
finally
{
System.out.println("Finally");
}
System.out.println("End");
}
结果为Begin
public static void main(String[] args) throws Exception
{
try
{
System.out.println("Begin");
A(0); //抛出异常
System.exit(0);
}
catch(Exception e)
{
System.out.println("Wrong");
throw e;
}
finally
{
System.out.println("Finally");
}
System.out.println("End");
}
输出结果为
Begin
Wrong
Finally
因为main方法已经抛出了异常,所以异常终止,不会继续执行下去。
假如注释掉throw e;这条语句
public static void main(String[] args) throws Exception
{
try
{
System.out.println("Begin");
A(0); //抛出异常
System.exit(0);
}
catch(Exception e)
{
System.out.println("Wrong");
//throw e;
}
finally
{
System.out.println("Finally");
}
System.out.println("End");
}
输出结果为
Begin
Wrong
Finally
End
(2)假如含有return语句,那么finally会在return之前执行。
public void A(int x) throws Exception
{
if (x == 0)
throw new Exception("Zero"); //
} piblic int B(int x)
{
try
{
System.out.println("Begin"); //
A(x); //
return 1;
}
catch(Exception e)
{
System.out.println(e.getMessage()); //
return 0; //
}
finally
{
System.out.println("Finally"); //
}
} public static void main(String[] args)
{
System.out.println(B(0)); //
}
输出结果为
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类
public class S extends Thread
{
public void run()
{
//do sth;
}
} S s = new S();
s.start(); //start()会自动调用run()
2.实现Runnable接口
public class S implements Runnable
{
public void run()
{ }
} S s = new S();
Thread thread = new Thread(s);
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,指定缓冲区大小
FileOutputStream out1 = new FileOutputStream("C:\\test.txt");
//装饰文件输出流
BufferedOutputStream out2 = new BufferedOutputStream(out1);
//装饰带缓冲的文件输出流
DataOutputStream out3 = new DataOutputStream(out2);
out3.writeByte(-12);
out3.flush(); //无论缓冲区是否满,都输出
out3.writeDouble(1.2);
out3.writeChar('a');
out3.close(); //close()会自动调用flush()
InputStream in1 = new InputStream("C:\\test.txt");
//装饰文件输入流
BufferedInputStream in2 = new BufferedInputStream(in1);
//装饰带缓冲的文件输入流
DataOutputStream in3 = new DataOutputStream(in2);
int a = in3.readByte();
double b = in3.readDouble();
char c = in3.readChar();
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)
InputStream in1 = new FileInputStream("C:\\test.txt");
InputStreamReader in2 = new InputStreamReader(in1, "UTF-8");
BufferedReader in3 = new BufferedReader(in2);
StringWriter buffWriter = new StringWriter();
int data;
while ((data = in3.read()) != -1)
buffWriter.write(data);
in3.close;
String result = buffWriter.toString();
若有编码的问题,则应当考虑使用Reader/Writer而不是InpuStream/OutputStream.
Java逍遥游记读书笔记<三>的更多相关文章
- Java逍遥游记读书笔记<一>
前言 必须先来一句,这是入门级别,高手勿喷~ 写Android的时候总有一些语句不是很理解,其实大部分是Java的内容,所以想系统的学下Java. 这本书——<Java逍遥游记>是在图书馆 ...
- Java逍遥游记读书笔记<二>
Abstract抽象类 1.抽象类不能被实例化 2.抽象方法没有方法体 如: public abstract class Weapen { public abstract void attack(); ...
- java编程思想读书笔记三(11-21)
十一:持有对象 >持有对象实例 ●数组将数字与对象联系起来.它保存类型明确的对象,查询对象时,不需要对结果做类型转换.他可以是多维的. 可以保存基本的数据类型.但是,数组一旦生成,容量就不会在变 ...
- 深入理解Java虚拟机之读书笔记三 内存分配策略
一般的内存分配是指堆上的分配,但也可能经过JIT编译后被拆散为标量类型并间接地在栈上分配.对象主要分配在新生代的Eden区上,如果启动了本地线程分配缓冲,将按线程优先在TLAB上分配,少数情况下直接分 ...
- 《深入理解java虚拟机》读书笔记三——第四章
第四章 虚拟机性能监控与故障处理工具 1.JDK命令行工具 jps命令: 作用:列出正在运行的虚拟机进程. 格式:jps [option] [hostid] 选项:-q 只输出LVMID(Local ...
- 《深入理解Java虚拟机》读书笔记三
第四章 虚拟机性能监控与故障处理工具 1.JDK命令行工具 jps命令: 作用:列出正在运行的虚拟机进程. 格式:jps [option] [hostid] 选项:-q 只输出LVMID(Local ...
- JAVA编程思想读书笔记(三)--RTTI
接上篇JAVA编程思想读书笔记(二) 第十一章 运行期类型判定 No1: 对于作为程序一部分的每个类,它们都有一个Class对象.换言之,每次写一个新类时,同时也会创建一个Class对象(更恰当的说, ...
- JAVA编程思想读书笔记(四)--对象的克隆
接上篇JAVA编程思想读书笔记(三)--RTTI No1: 类的克隆 public class MyObject implements Cloneable { int i; public MyObje ...
- 《大型网站系统与Java中间件》读书笔记 (中)
前言 只有光头才能变强. 文本已收录至我的GitHub仓库,欢迎Star:https://github.com/ZhongFuCheng3y/3y 回顾上一篇: <大型网站系统与Java中间件& ...
随机推荐
- ASCII、Unicode、UTF8编码类型的理解
一.ASCII码 在计算机内部,所有的信息最终都表示为一个二进制的字符串.每一个二进制位(bit)有0和1两种状态,因此八个二进制位就可以组合出256种状态,这被称为一个字节(byte) ...
- 代码可读性艺术在Andorid中的体现
前言 最近接手的一些项目,不同的人编码风格迥异,类里的变量.方法的定义穿插,注释极为稀少,更有一些变量和方法的命名非常近似,例如表示播放队列的"playQueue"和表示歌单的&q ...
- Unity3d_ADBannerView
原地址:http://blog.csdn.net/cynixway/article/details/7686393 ADBnnerView提供对Apple iAd框架中ADBannerView的包中, ...
- [Spring MVC - 2A] - java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
严重: Servlet.service() for servlet [springMVC] in context with path [/ExceptionManageSystem] threw ex ...
- 【Linux】通过传入变量进行数学运算
一个简单的sum求和 #! /bin/bash ## For get the sum of tow numbers ## Writen by Qinys ## Date:2018-06-26 a=1 ...
- Unity 背包道具搜索
因为背包有很多道具,用户要根据不同需要搜索出不同的道具. 道具的属性有非常居多,游戏快开发完毕的时候,突然发现ItemManager类里面几乎每一个搜索方法都有一个foreach循环, 循环里面因为 ...
- 关于org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor的队列
今天查看源码发现spring的线程池是支持队列的: 并且队列支持的上限相当大: 当线程池的达到最大线程时,默认会把任务放在队列(内存)中,所以我们可以放心用这个东西来写日志了
- MongoDB 操作手冊CRUD 删除 remove
删除记录 概述 在MongoDB中,db.collection.remove()方法用于删除集合中的记录.能够删除全部记录,删除全部符合条件的记录.或者是仅删除一条记录. 删除全部记录 删除一个集合中 ...
- jboss 的debug启动4法
http://xo-tobacoo.iteye.com/blog/684946方式一: 使用myeclipse,全自动化,不再赘述 方式二: eclipse下使用server工具,部署后使用debug ...
- 在windows 2012中安装sharepoint 2013时遇到问题的处理办法
众所周知,sharepoint 2013是早于windows 2012的,所以在安装的时候,总会出现各种奇怪的问题,也就是所谓的一个个坑,为了减少大家掉到坑里的次数和排除故障的时间,我在这里记录下我曾 ...