学习笔记 - java 异常的捕获及处理
*看视频整理,仅用于自己学习,侵删
1.异常
*异常是导致程序中断运行的一种指令流,如果不对异常进行正确的处理,,可能导致程序的中断执行,造成不必要的损失,所以在java程序设计时,要充分地利用java的异常处理机制
案例:
public class ExceptionDemo01{
public static void main(String args[]) {
System.out.println("****** 计算开始 ");
int i = 10;
int j = 0;
int temp = i / j;
System.out.println("tenmp = " + temp);
System.out.println(" 计算结束 ******");
}
}
被除数是0意味着无穷大,将会占满所有内存
2.异常处理的格式
try{
//有可能出现异常的语句
}catch(异常类 异常对象){
//编写异常的处理语句
}[ catch (异常类 异常对象){
//编写异常的处理语句
}…]
[finally{
一定会运行到的程序代码
}]
在try语句之中捕获可能出现的异常代码。如果在try中 产生了异常,则程序会自动跳转到catch语句中找到匹配的异常类型进行相应的处理。最后不管程序是否会产生异常,则肯定都会执行到finally语句,finally语句就作为异常的统一 岀口。需要提醒读者的是,finally块是可以省略的。如果省略了 finally块,则在catch()块 运行结束后,程序会跳到try-catch块之后继续执行。
案例:
public class ExceptionDemo04{
public static void main(String args[]) {
System.out.println(“计算开始”);
int i = 0;
int j = 0;
try {
String str1 = args[0];
String str2 = args[1];
i = Integer.parseInt(str1); //将字符串变为整形
j = Integer.parseInt(str2);
int temp = i/j;
System.out.println(“两个数字相除结果:” + temp);
System.out.println("-----------");
}catch(ArithmeticException e) { //捕捉算数异常
System.out.println(“算术异常” + e); //出现异常执行异常处理语句
}catch(NumberFormatException e) {
System.out.println(“数字转换异常” + e);
}catch(ArrayIndexOutOfBoundsException e) {
System.out.println(“数组越界异常” + e);
}finally {
System.out.println(“运行了finally”);
}
System.out.println("计算结束");
}
}
[不输入数值]
[输入了10 5]
[输入了 a b ]
- 捕捉异常
① 数组超出绑定异常:ArraylndexOutOfBoundsException□
② 数字格式化异常:NumberFormatException□
③ 算术异常:ArithmeticException。
3.异常类的继承结构
- 一般情况下,将Exception和Error统称为异常,而算术异常、数字格式化异常等都属于Exception的子类。
*catch语句输出异常1.System.out.println(异常对象)
2.Exception类中的printStackTrace()方法
例:e.printStackTrace () //异常信息最完整
4.Java的异常处理机制
(1) 一旦产生异常,则首先会产生一个异常类的实例化对象。
(2) 在try语句中对此异常对象进行捕捉。
(3) 产生的异常对象与catch语句中的各个异常类型进行匹配,如果匹配成功,则执行catch语句中的代码。
案例
public class ExceptionDemo05{
public static void main(String args[]) {
System.out.println(“计算开始”);
int i = 0;
int j = 0;
try {
String str1 = args[0];
String str2 = args[1];
i = Integer.parseInt(str1); //将字符串变为整形
j = Integer.parseInt(str2);
int temp = i/j;
System.out.println(“两个数字相除结果:” + temp);
System.out.println("-----------");
}catch(ArithmeticException e) { //捕捉算数异常
System.out.println(“算术异常” + e); //出现异常执行异常处理语句
}catch(NumberFormatException e) {
System.out.println(“数字转换异常” + e);
}catch(ArrayIndexOutOfBoundsException e) {
System.out.println(“数组越界异常” + e);
}
catch(Exception e) {
System.out.println(“其他异常” + e); //大范围的异常捕捉放在小范围之后,否则会报错
}finally {
System.out.println(“运行了finally”);
}
System.out.println("计算结束*");
}
}
案例
public class ExceptionDemo06{
public static void main(String args[]) {
System.out.println(“计算开始”);
int i = 0;
int j = 0;
try {
String str1 = args[0];
String str2 = args[1];
i = Integer.parseInt(str1); //将字符串变为整形
j = Integer.parseInt(str2);
int temp = i/j;
System.out.println(“两个数字相除结果:” + temp);
System.out.println("-----------");
}
catch(Exception e) {
System.out.println(“其他异常” + e); //大范围的异常捕捉
}
System.out.println("计算结束*");
}
}
在一个程序中,如果有多个异常最好还是分别进行捕捉,而不要直接使用Exception捕捉全部异常
5.throws关键字
*使用throws声明的方法表示此方法不处理异常,而交给方法的调用处进行处理,throws使用格式如下
public 返回值类型 方法名称(参数列表…) throws 异常类{ }
案例:
class Math{
public int div(int i,int j) throws Exception{ //无论是否有异常,在调用此方法前都要进行异常处理
int temp = i /j ;
return temp;
}
};
public class ThrowsDemo01{
public static void main(String args[]) {
Math m = new Math();
try {
System.out.println(“除法操作 :” + m.div(10,0));
}catch(Exception e) {
e.printStackTrace(); //打印异常
}
}
}
在以上代码中,是否有问题,都要用try…catch进行异常的捕获与处理
案例2
class Math{
public int div(int i,int j) throws Exception{ //无论是否有异常,在调用此方法前都要进行异常处理
int temp = i /j ;
return temp;
}
};
public class ThrowsDemo02{
public static void main(String args[]) throws Exception {
Math m = new Math();
System.out.println("除法操作" + m.div(10,0));
}
}
6.throw关键字
与throws不同的是,可以直接使用throw抛出一个异常,抛出时直接抛出异常类的实 例化对象即可。
范例:抛出异常
public class ThrowDemo01{
public static void main(String args[]) {
try {
throw new Exception(“自己抛出的异常!”);
}catch(Exception e) {
System.out.println(e);
}
}
}
案例3:
throws和throw联合使用
class Math{
public int div(int i,int j) throws Exception{ //无论是否有异常,在调用此方法前都要进行异常处理
System.out.println(“计算开始 ***** ");
int temp = 0;
try {
temp = i /j;
}catch(Exception e) {
throw e; // 此处不这样操作,计算结束将无法出现
}finally {
System.out.println(" 计算结束 *****”);
}
return temp;
}
};
public class ThrowDemo02{
public static void main(String args[]) {
Math m = new Math();
try {
System.out.println(“除法操作 :” + m.div(10,0));
}catch(Exception e) {
System.out.println(“异常产生:” + e);
}
}
}
7.Exception类与RuntimeException类(面试题)
*Exception在程序中必须使用try…catch进行处理。
RuntimeException可以不使用try…catch进行处理,但是如果有异常产生,则异常 将由JVM进行处理。
对于RuntimeException的子类最好也使用异常处理机制。
虽然RuntimeException的异常可以不用try…catch进行处理,但是如果一旦出现异常,则肯定会导致程序中断执行,所以,为了保证程序在出错后依然可以执行,所以在开发代码时 最好使用try-catch的异常处理机制进行处理。
8.自定义异常类
- 只要继承Exception类就可
- class MyException extends Exception{ //自定义异常类,继承Exception方法
public MyException(String msg) { //构造方法接收异常信息
super(msg);
}
}
public class DefaultException{
public static void main(String args[]) {
try {
throw new MyException(“自定义异常”);
}catch(Exception e) {
e.printStackTrace();
}
}
}
9.断言
断言就是肯定某一个结果的返回值是正确 的,如果最终此结果的返回值是错误的,则通过断言检查肯定会提示错误信息。
格式:
assert boolean 表达式;
assert boolean 表达式 :详细信息
- 案例
*public class Test{
public static void main(String args[]) {
int x[] = {1,2,3} ;
assert x.length ==0 : “数组长度为0”; //在此处断言数组长度为0
}
}
在翻译程序时要加上 -ea (-enableassertions)
注:断言的返回值不能作为条件语句判断句
在开发中不提倡使用断言,了解即可