码迷,mamicode.com
首页 > 编程语言 > 详细

Java异常(二)

时间:2018-11-05 01:14:46      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:处理   报错   错误   result   试题   fbo   演示   mes   必须   

**********************为什么有finally*************************

package com.chapter10;

//演示发生了异常没捕获的情况
public class TestFinally {

public static void main(String[] args) {

try{
int a = 0;//连接数据库
int result = 10/a;
System.out.println("aaaaa");
}catch(ArrayIndexOutOfBoundsException e){
e.printStackTrace();
System.out.println("数组下标越界");
}catch(NullPointerException e){
e.printStackTrace();
System.out.println("除数不能为0");
}catch(ClassCastException e){
e.printStackTrace();
System.out.println("类型转换错误");
}finally{

//无论如何总要被执行 除非系统强制退出
System.out.println("关闭数据库");
}

//报错终止



System.out.println("here");
}

}


程序发生异常后,没有捕获 会报错终止, 这样后面的代码就没有机会执行了(例如关闭数据库的代码)..


这样光申请资源而不释放肯定是有问题的, 所以为了保证某些代码 必须被执行 可以把这些代码 放到
finally代码块中, finally代码块中的代码 无论如何都会执行...


一般finally 中放一些释放资源的代码 例如关闭数据库、关闭io流、关闭socket通道等..

 

关于finally的几个面试题:


try中如果有return 不会立马返回, 先执行finally,如果finally中有return 那么整个方法的返回值取finally中的返回值,


如果finally 中没有return ,那么整个方法的返回值取try中的..

 


*********************try后面必须跟catch 或finally其中一个*************************


package com.chapter10;

//try后面必须跟catch或finally其中之一
public class TestTryPrinciple {

public static void main(String[] args) {


try {
int a = 10;
int b = a/0;
} catch (Exception e) {
System.out.println("算数异常");
e.printStackTrace();
}

}
}

 


try {
int a = 10;
int b = a/0;
} finally{
System.out.println("释放资源的代码");
}

 

*************************************快捷键*************************


选中需要加try...catch的代码块 右键 surround with ---try...catch

 

 

****************************处理检查性异常的第二种方式--使用throws抛出给调用者处理********************

 

1. 使用try...catch明确处理


驾驶员自己修


2. 使用throws 抛出给调用者处理


坐席碰到异常以后 有两种处理方式,如果能处理 使用try..catch明确处理,如果处理不了,使用throws抛给调用者处理(上级),


抛给班长坐席,班长坐席收到这个异常以后,又有两种方式:try..catch 和使用throws继续抛

 

演示代码:

package com.chapter10.handlerCheckedException2;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

//演示处理检查性异常的第二种方式
//使用throws抛给调用者处理
public class TestHandleCheckedException2 {

//警察
//main方法中肯定要处理异常,就像警察必须处理异常一样
//如果main方法不做任何处理 继续向上抛出 会抛给JVM(不推荐)
//JVM的默认处理方式: 打印栈轨迹
public static void main(String[] args) throws FileNotFoundException{
TestHandleCheckedException2 test = new TestHandleCheckedException2();

test.laoliu();
}

public void laoliu() throws FileNotFoundException {

manager();

}

public void manager() throws FileNotFoundException {

customerAgentLeader();
}

// 班长坐席
public void customerAgentLeader() throws FileNotFoundException {
customerAgent();
}

public void customerAgent() throws FileNotFoundException {

// 碰到一个异常
FileInputStream fis = new FileInputStream("d:\\jidi16\\aaa.txt");
}

}


********************************查看API**************************************

public FileInputStream(String name)
throws FileNotFoundException

 

表示该构造函数可能会出现FileNotFoundException异常,并且该方法本身不做处理,抛给调用者处理

使用该类new对象的人需要进行处理或者继续使用throws抛出

 

********************************自定义异常**************************************

如果API提供的异常不够用,需要用户自己自定义异常


需求: 自定义一个 想要换手机的异常

 


throw 产生了一个异常 异常发生了 相当于 10/0

 

演示代码:


package com.chapter10.演示自定义异常;

//自定义的异常类
//1.继承Exception
//2.使用super("异常的具体描述") 初始化异常描述

//用户自己自定义的异常 都是检查性异常
public class WannaChangePhone extends Exception {

private String name;
private String reason;

public WannaChangePhone() {
super("想要换手机");
}

public WannaChangePhone(String name, String reason) {
super("想要换手机");
this.name = name;
this.reason = reason;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getReason() {
return reason;
}

public void setReason(String reason) {
this.reason = reason;
}

}

 


package com.chapter10.演示自定义异常;

//坐席
public class CustomerAgent {


public void getPhone() throws WannaChangePhone{

if(true){

throw new WannaChangePhone("丁泽鹏","买到二手手机");//产生了一个异常

}
}

public static void main(String[] args) {
CustomerAgent ca = new CustomerAgent();

try {
ca.getPhone();
} catch (WannaChangePhone e) {
System.out.println(e.getName() + "因为" + e.getReason() + e.getMessage());
e.printStackTrace();
}
}
}

 


********************************throws和throw的区别**************************************


throws 处理检查性异常的第二种方式: 抛给调用者处理, 调用者收到这个异常以后 又有两种处理方式:
1.使用try..catch明确的处理 2. 使用throws 继续向上抛

 

throw 产生了一个异常 异常发生了 相当于10/0

*****************************异常的若干知识点***************************

1. 当有多个catch块时, 范围大的异常类应该放到后面....

 

instanceof:

引用a instanceof 类b : 如果引用a 引用的是类b或类b子类的对象返回true,否则返回false

 

2. 不要使用Exception获取所有异常,虽然这样写代码简单,但是不能针对不同的异常做不同的处理...区分不开

 


***************************补充重写规则之两小**********************************

1. 子类方法抛出的异常 是 父类方法抛出异常的子类

 

Java异常(二)

标签:处理   报错   错误   result   试题   fbo   演示   mes   必须   

原文地址:https://www.cnblogs.com/MrTanJunCai/p/9906798.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!