版权声明:本文为博主原创小文章,转载请联系我,邮箱[email protected] https://blog.csdn.net/qq_39210208/article/details/86494758
异常(未完待续)
是在运行时期发生的不正常情况。。
在java中用类的形式对不正常情况进行了描述和封装对象。
描述不正常的情况的类,就称为异常类。
以前正常流程代码和问题处理代码相结合,
现在将正常流程代码和问题处理代码分离。提高阅读性.
其实异常就是java通过面向对象的思想将问题封装成了对象.
用异常类对其进行描述。
不同的问题用不同的类进行具体的描述。 比如角标越界。空指针等等。
问题很多,意味着描述的类也很多,
将其共性进行向上抽取,形成了异常体系。
最终问题(不正常情况)就分成了两大类。
Throwable:无论是error,还是异常,问题,问题发生就应该可以抛出,让调用者知道并处理。
//该体系的特点就在于Throwable及其所有的子类都具有可抛性。
可抛性到底指的是什么呢?怎么体现可抛性呢?
其实是通过两个关键字来体现的。
throws throw ,凡是可以被这两个关键字所操作的类和对象都具备可抛性.
|--1,一般不可处理的。Error
特点:是由jvm抛出的严重性的问题。
这种问题发生一般不针对性处理。直接修改程序
|--2,可以处理的。Exception
该体系的特点:
子类的后缀名都是用其父类名作为后缀,阅读性很强。
class Demo {
public int method(int[] arr, int index) {
if (arr == null)
throw new NullPointerException("数组的引用不能为空!");
if (index >= arr.length) {
throw new ArrayIndexOutOfBoundsException("数组的角标越界啦:" + index);
}
if (index < 0) {
throw new ArrayIndexOutOfBoundsException("数组的角标不能为负数:" + index);
}
return arr[index];
}
}
class ExceptionDemo2 {
public static void main(String[] args) {
int[] arr = new int[3];
Demo d = new Demo();
int num = d.method(null, -30);
System.out.println("num=" + num);
System.out.println("over");
}
}
对于角标是整数不存在,可以用角标越界表示,
对于负数为角标的情况,准备用负数角标异常来表示。
负数角标这种异常在java中并没有定义过。
那就按照java异常的创建思想,面向对象,将负数角标进行自定义描述。并封装成对象。
这种自定义的问题描述成为**自定义异常**。
注意:如果让一个类称为异常类,必须要继承异常体系,因为只有称为异常体系的子类才有资格具备可抛性。
才可以被两个关键字所操作,throws throw
异常的分类:
1,编译时被检测异常:只要是Exception和其子类都是,除了特殊子类RuntimeException体系。
这种问题一旦出现,希望在编译时就进行检测,让这种问题有对应的处理方式。
这样的问题都可以针对性的处理。
2,编译时不检测异常(运行时异常):就是Exception中的RuntimeException和其子类。
这种问题的发生,无法让功能继续,运算无法进行,更多是因为调用者的原因导致的而或者引发了内部状态的改变导致的。
那么这种问题一般不处理,直接编译通过,在运行时,让调用者调用时的程序强制停止,让调用者对代码进行修正。
所以自定义异常时,要么继承Exception。要么继承RuntimeException。
throws 和throw的区别。
1,throws使用在函数上。
throw使用在函数内。
2,throws抛出的是异常类,可以抛出多个,用逗号隔开。
throw抛出的是异常对象。
异常处理的捕捉形式:
这是可以对异常进行针对性处理的方式。
具体格式是:
try{
//需要被检测异常的代码。
}
catch(异常类 变量){ //该变量用于接收发生的异常对象
//处理异常的代码。
}
finally{
//一定会被执行的代码。
}
异常处理的原则:
1,函数内容如果抛出需要检测的异常,那么函数上必须要声明。
否则必须在函数内用trycatch捕捉,否则编译失败。
2,如果调用到了声明异常的函数,要么try catch要么throws,否则编译失败。
3,什么时候catch,什么时候throws 呢?
功能内容可以解决,用catch。
解决不了,用throws告诉调用者,由调用者解决 。
4,一个功能如果抛出了多个异常,那么调用时,必须有对应多个catch进行针对性的处理。
内部又几个需要检测的异常,就抛几个异常,抛出几个,就catch几个。
class FuShuIndexException extends Exception {
FuShuIndexException() {
}
FuShuIndexException(String msg) {
super(msg);
}
}
class Demo2 {
public int method(int[] arr, int index) throws FuShuIndexException{// throws NullPointerException,
//FuShuIndexException
if (arr == null)
throw new NullPointerException("没有任何数组实体");
if (index < 0)
throw new FuShuIndexException();
return arr[index];
}
}
class ExceptionDemo4 {
public static void main(String[] args) {
int[] arr = new int[3];
Demo2 d = new Demo2();
try {
int num = d.method(arr, -1);
System.out.println("num=" + num);
}
catch (NullPointerException e) {
System.out.println(e.toString());
} catch (FuShuIndexException e) {
System.out.println("message:" + e.getMessage());
System.out.println("string:" + e.toString());
e.printStackTrace();// jvm默认的异常处理机制就是调用异常对象的这个方法。
System.out.println("负数角标异常!!!!");
}
/*
* catch(Exception e) { //多catch父类的catch放在最下面。
* }
*/
System.out.println("over");
}
}
运行结果:
message:null
string:FuShuIndexException
FuShuIndexException
at Demo2.method(ExceptionDemo4.java:59)
at ExceptionDemo4.main(ExceptionDemo4.java:70)
负数角标异常!!!!
over
class Demo5 {
public int show(int index) throws ArrayIndexOutOfBoundsException {
if (index < 0)
throw new ArrayIndexOutOfBoundsException("越界啦!!");
int[] arr = new int[3];
return arr[index];
}
}
class ExceptionDemo5 {
public static void main(String[] args) {
Demo5 d = new Demo5();
try{
int num = d.show(-1);
System.out.println("num="+num);
}
catch (ArrayIndexOutOfBoundsException e){
System.out.println(e.toString());
// return ;
// System.exit(0);//退出jvm。
}
finally{ //通常用于关闭(释放)资源。
System.out.println("finally");
}
System.out.println("over");
}
}
执行结果:
java.lang.ArrayIndexOutOfBoundsException: 越界啦!!
finally
over
连接数据库 查询。Exception 关闭连接:
class NoAddException extends Exception{ }
void addData(Data d)throws NoAddException{
连接数据库
try{
添加数据。出现异常 SQLException();
}
catch(SQLException e){
//处理代码。
throw new NoAddException();
}
fianlly{
关闭数据库。
}
}
try catch finally 代码块组合特点:
1,try catch finally
2,try catch(多个)当没有必要资源需要释放时,可以不用定义finally。
3,try finally 异常无法直接catch处理,但是资源需要关闭。
电脑蓝屏冒烟
class LanPingException extends Exception {
LanPingException(String msg) {
super(msg);
}
}
class MaoYanException extends Exception {
MaoYanException(String msg) {
super(msg);
}
}
class NoPlanException extends Exception {
NoPlanException(String msg) {
super(msg);
}
}
class Computer {
private int state = 2;
public void run() throws LanPingException, MaoYanException {
if (state == 1)
throw new LanPingException("电脑蓝屏啦!!");
if (state == 2)
throw new MaoYanException("电脑冒烟啦!!");
System.out.println("电脑运行");
}
public void reset() {
state = 0;
System.out.println("电脑重启");
}
}
class Teacher {
private String name;
private Computer comp;
Teacher(String name) {
this.name = name;
comp = new Computer();
}
public void prelect() throws NoPlanException {
try {
comp.run();
System.out.println(name + "讲课");
} catch (LanPingException e) {
System.out.println(e.toString());
comp.reset();
prelect();
} catch (MaoYanException e) {
System.out.println(e.toString());
test();
// 可以对电脑进行维修。
// throw e;
throw new NoPlanException("课时进度无法完成,原因:" + e.getMessage());
}
}
public void test() {
System.out.println("大家练习");
}
}
class ExceptionTest {
public static void main(String[] args) {
Teacher t = new Teacher("老师");
try {
t.prelect();
} catch (NoPlanException e) {
System.out.println(e.toString() + "......");
System.out.println("换人");
}
}
}
.
.
class ExceptionTest2 {
public static void main(String[] args) {
int num = show();
System.out.println("num=" + num);
}
public static int show() {
int aa = 0;
try {
System.out.println("show run");
if (1 == 1)
throw new RuntimeException("AAA");
aa = 10;
return aa;
} catch (RuntimeException e) {
System.out.println("catch....." + e.toString());
aa = 20;
// finally
return 20;
} finally {
System.out.println("finally...."+aa);
aa = 30;
System.out.println("finally-------"+aa);
return aa;
// throw new RuntimeException("finally runtime........");
}
}
}
输出结果:
show run
catch.....java.lang.RuntimeException: AAA
finally....20
finally-------30
num=30