JavaSE学习笔记 Day17
个人整理非商业用途,欢迎探讨与指正!!
« 上一篇
文章目录
- JavaSE学习笔记 Day17
- ···
- 十五、异常
- 15.1异常的概念
- 15.2异常处理的重要性
- 15.3Throwable类
- 15.4Exception类
- 15.4.1运行期异常
- 15.4.2编译期异常
- 15.5异常处理机制
- 15.5.1抓
- 15.5.2抓模型的细节
- 15.5.3抛
- 15.5.3.1throws
- 15.5.3.2throw
- 15.5.3.3抓抛的选取
- 15.5.3.3为什么要手动的throw异常?
- 15.6自定义异常
- 15.7方法重写和重载中的异常
- 十六、集合
- 16.1集合的概念
- 16.2单列集合
- 16.2.1Collection接口
- 16.2.1.1常用方法
···
十五、异常
15.1异常的概念
程序运行过程中出现的错误,称为异常
15.2异常处理的重要性
若异常不处理,那么就会中断JVM的运行.异常之后的代码不会再执行了
public class Demo01 {public static void main(String[] args) {
// System.out.println(10 / 0);int[] arr = new int[0];
// System.out.println(arr[0]);Object obj = "";
// int i = (Integer) obj;String str = null;
// System.out.println(str.length());set(str);System.out.println("我还能被执行了吗");}public static void set(String str) {System.out.println(str.length());}
}
15.3Throwable类
未抛出的,未测试的
是所有异常(Exception)和错误(Error)的父类
Throwable对象(子类对象)
1.是由jvm在捕捉到异常信息是创建的
2.是由throw创建
15.4Exception类
是java中所有异常的父类
java中所有的已知异常都是由Exception结尾的
java中若发现两个中,一类内部包含另一个类的名字,那么被包含的类名往往就是父类/接口
例如:ArrayIndexOutOfBoundsException的父类是IndexOutOfBoundsException
java中的异常可以为分两类:
运行期异常:运行过程中可能会出现的异常
编译期异常:编码过程中必须进行处理的异常
15.4.1运行期异常
运行期异常,可以不用处理,系统自动的交给jvm处理
运行期异常产生之后,异常之下的代码是无法执行的
运行期异常是偶然发生的,有可能的
常见的运行期异常
RuntimeException:运行期异常,是所有的运行异常的父类
AtithmeticException:数学异常,除0时会发生
ClassCaseException:类型转换异常,向下造型时会发生
IllegalArgumentExcepion:非法参数异常
NumberFormatException:数字转换异常,字符串转换为数值时会发生
IndexOutOfBoundsException:下标越界异常
ArrayIndexOutOfBoundsException:数组下标越界异常,下标不在指定范围内
StringIndexOutOfBoundsException:字符串下标越界异常
NullPointerException:空值异常,使用null值打点调用时发生
15.4.2编译期异常
方法自带的异常(普通方法,构造方法)
编译期异常必须被处理,否则编码无法通过
除了运行期异常都是编译异常,都需要进行处理
常见的编译期异常
SQLException:一切和SQL相关的java操作,都会抛出
IOException:一切IO操作(磁盘的读写),都会抛出
public class Demo05 {public static void main(String[] args) throws SQLException {
// 两种处理方案:1.try/catchtry {test();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}
// 2.throwstest();}// 该方法自带编译异常,必须被处理public static void test() throws SQLException {}
}
15.5异常处理机制
java提供了两种异常处理机制:①抓②抛
java中的异常处理抓抛模型
15.5.1抓
捕获异常,主动的去处理
语法:
try{
//可能会出现异常的代码
}catch(异常对象 e){
//当出现对应的异常对象时,执行的代码
}…[多个catch]
//可以有多个异常对象
}finally{
//无论有无异常产生都会执行的代码
}
特点:
try块不能独立使用,需要和catch或finally一起使用(多数情况下是三个块一起使用)
public class Demo06 {public static void main(String[] args) {System.out.println("我是main的开始");try {
// try中的代码可能会有异常//System.out.println(10 / 0);//会产生一个异常对象,这个对象是JVM创建的(ArithmeticException)String str = null;System.out.println(str.length());
// 异常之下代码无法执行System.out.println("我是异常之下");}catch(ArithmeticException e/*类似于形参,捕获到的对象为e进行赋值*/) {System.out.println("这里是抓到数学异常进行的输出");}catch (NullPointerException e) {System.out.println("这里是抓到空值异常进行的输出");}finally {System.out.println("无论有无异常,都会执行");}//有了异常处理之后,不影响后续的代码执行了System.out.println("我是main的结尾");}
}
15.5.2抓模型的细节
抓取异常时,若有多个异常,会抓到第一个进行处理
public class Demo07 {public static void main(String[] args) {
// 抓到程序运行时的第一个异常try {System.out.println(10 / 0);String str = null;System.out.println(str.length());} catch (ArithmeticException e) {System.out.println("数学异常");} catch (NullPointerException e) {System.out.println("空值异常");}}
}
三个块往往是一起使用,try可以和catch或者finally单独使用
// try和catch一起使用
try {} catch (Exception e) {// TODO: handle exception
}
// try和finally一起使用
try {} finally {// TODO: handle finally clause
}
异常常用的方法
public class Demo09 {public static void main(String[] args) {
// System.out.println(10 / 0);try {System.out.println(10 / 0);} catch (Exception e) {
// 异常处理机制是独立线程的
// 打印异常的堆栈信息e.printStackTrace();
// 异常的局部信息System.out.println(e.getMessage());}System.out.println("最后");}
}
捕获异常的顺序
若异常之间有父子关系,优先于子异常,后捕获父异常
没有父子关系的,顺序无所谓
try {} catch (NullPointerException e) {} catch (RuntimeException e) {} catch (Exception e) {} catch (Throwable e) {}// 同时抓捕多个异常,多个异常之间是没有关系的
try {} catch (NullPointerException e) {} catch (ArithmeticException e) {}try {} catch (NullPointerException|ArithmeticException e) {}
15.5.3抛
被动处理
15.5.3.1throws
在方法的声明处使用throws关键字,表示当前方法携带异常
throws表示当前的异常不被处理,交给调用者,若在main中仍然throws交给JVM
public class Demo11 {public static void main(String[] args) throws Exception {
// 运行期可以不处理test01();
// 编译期异常被调用时,必须处理,处理方案:1抓 2抛
// 抓try {test02();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}
// 抛test02();}
// throws运行期异常
// 运行期异常,默认可以不被处理的public static void test01() throws RuntimeException {}
// throws编译期异常
// 编译期异常是必须再次被处理的public static void test02() throws Exception {}
}
15.5.3.2throw
在方法内部抛出异常
没有异常手动创建异常对象
若throw的异常为运行期异常可以不处理,但为编译期异常必须处理
public class Demo12 {// 方法内部抛出运行期异常public static void test01() {
// 手动创建的throw new RuntimeException("我是异常");}// 方法内部抛出编译期异常,就必须处理掉public static void test02() throws Exception {
// 处理编译期异常:1.抓 2.抛
// 1.try {throw new Exception();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}
// 2.throw new Exception();}public static void main(String[] args) {try {test01();} catch (RuntimeException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println("结束");}
}
15.5.3.3抓抛的选取
能抓就抓,遇到异常我们首先都是主动抓取
但在程序设计时,接口中的抽象方法是无法被抓取的,选择抛
public interface MyInter {void add() throws SQLException;void del() throws SQLException;void up() throws SQLException;
}
15.5.3.3为什么要手动的throw异常?
场景:取钱时余额不足,用户名密码错误
public class Demo13 {double balance = 100000000;
// 取款public double getMoney(int balance) {if(balance < 0 ) {
// 某种程度上,throw new 异常可以代替returnthrow new RuntimeException("输入有误");}if(balance > this.balance) {throw new RuntimeException("余额不足");}return this.balance - balance;}public static void main(String[] args) {Demo13 d13 = new Demo13();// d13.getMoney(-100);
// d13.getMoney(100000001);System.out.println(d13.getMoney(10));}
}
15.6自定义异常
已知的异常种类是有限的,遇到问题了需要具体的异常信息,可以自定义异常
继承已知异常,添加对应的构造方法即可
自定义运行期异常继承RuntimeException
自定义编译期异常继承Exception
15.7方法重写和重载中的异常
重载和异常没有任何关系(同名不同参)
重写:
和运行期异常没有关系
编译期异常,子类抛出的异常不得宽于父类
public class Demo14 {public void test01() throws SQLException{}public void test02() throws Exception {}
}class A extends Demo14 {
// 不符合抛出异常的要求/*@Overridepublic void test01() throws Exception {// TODO Auto-generated method stub}*/
// SQLNonTransientException是SQLException的子类@Overridepublic void test01() throws SQLNonTransientException {}@Overridepublic void test02() throws SQLException,IOException,Exception {}
}
十六、集合
16.1集合的概念
是对象的一种容器,用于存放对象
之前的容器是数组,数组是定长的,数组没有api,数组只能存储单一的类型数据
集合有大量的api,集合中可以存放任意Object类型,长度是可变的
集合的分类:单列集合和双列集合
16.2单列集合
Collecion接口
是所有单列集合的父接口,没有直接的实现类
子接口:
List接口:有序集合
Set接口:无序集合
实现类:
ArrayList,LinkedList,Vector是List的实现类
HashSet,LinkedHashSet,TreeSet是Set的实现类
16.2.1Collection接口
单列集合的最顶级父接口
JDK中不直接提供此接口的实现类
16.2.1.1常用方法
add(E e):向集合中添加内容
clear():清空集合中的所有内容
contains(Object o):判断当前的集合中是否有参数的数据
isEmpty():判断集合中的内容是否为空
remove(Object o):移除指定的元素
size():返回集合中元素的个数
toArray():将集合转换为数组
addAll(Collection<? extends E> c):在一个集合中添加另一个集合 containsAll(Collection<?> c):判断一个集合中是否包含另一个集合
removeAll(Collection<?> c):从一个集合中移除另一个集合
public class Demo01 {public static void main(String[] args) {
// 集合中不能添加基本数据类型
// 创建Collection的对象Collection coll = new ArrayList();
// 添加方法coll.add("hello");coll.add(new Integer(10));coll.add(20);//Integer类型的coll.add("bmh");
// 可以直接被输出System.out.println(coll.toString());
// 判断某个对象是否在集合中System.out.println(coll.contains("hello"));
// 判断集合是否为空System.out.println(coll.isEmpty());
// 清空集合/*coll.clear();System.out.println(coll);*/
// 长度System.out.println(coll.size());
// 转换为数组Object[] array = coll.toArray();System.out.println(Arrays.toString(array));
// 删除coll.remove("hello");System.out.println(coll);// 添加一个集合Collection c = Arrays.asList(10,20,30);//可以将数值直接转换为集合的coll.addAll(c);System.out.println(coll);// 判断是否包含一个数组System.out.println(coll.containsAll(c));
// 移除某个集合coll.removeAll(c);System.out.println(coll);}
}