Java–07
第四章 异常以及异常处理
目录
前言
21世纪,走进了信息时代,各种各样的软件层出不穷,但是总离不开程序开发,离不开程序开发语言,Java语言作为人们所熟知的一门语言,对于其有必要进行系统的学习。
异常
1.定义
异常,即不正常情况,在java代码中常会出现因某种特殊情况,而导致程序代码出现异常
的情况,在java中主要有Exception和Error两类,二者均为Throwable类的子类。
2.Exception
Exception其含义为异常,指程序所出现的,人为可控制的异常情况,其分为编译时异常
与运行时异常:
2.1 编译时异常
即在代码的编写过程中所出现的异常,目前的编译软件如Eclipse与IDEA等,在代码编
写时,均有语法验证,一般可规避编译时异常,如
2.2 运行时异常
即无编译时异常,但由于其他原因导致的异常如:
int a = 10, b = 0;
System.out.println(a / b);
定义两个int型整数,输出二者之商,由于除数不可为0,故其出现异常:
ArithmeticException,即数学异常;
2.2.1 常见的运行时异常
(1)ArithmeticException
int a = 10, b = 0;
System.out.println(a / b);
数学异常,由于数学的运算,如除数为0,而导致的异常;
(2)NumberFormatException
Integer.parseInt("a");
数字格式化异常,将其它类型转换为数字,如将String型的a解析为数字,而导致的异常;
(3)NullPointerException
String str=null;
int a= str.length();
空指针异常,对null进行操作,如获取值为null的String对象的长度,而导致的异常;
(4)ArrayIndexOutOfBoundsException
int[] arr={1,2,3};
System.out.println(arr[3]);
数组索引越界异常,对数组元素进行操作,但是超出数组索引范围,如获取值为索引为3
的int[]对象的值,而导致的异常;
2.2.2 运行时异常的处理
程序的编译时异常往往可以直接看到,从而避免,但是运行时异常往往不容易发现,故而,
对于程序执行过程中可能会出现的异常,需要进行处理,从而保证程序可以正常执行
(1)try…catch…
try {
int[] arr = {1, 2, 3};
System.out.println(arr[3]);
System.out.println("出现异常");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("处理异常---");
e.printStackTrace();
}
System.out.println("异常捕获后,代码可正常执行");
当try中代码出现异常,会由出现异常的行跳到catch中进行异常的捕获,执行catch中的
代码,并向下继续执行,但如果catch中所要捕获的类与发生异常的类不匹配,则不进行捕
获
try {
Integer.parseInt("a");
System.out.println("出现异常");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("处理异常---");
e.printStackTrace();
}
System.out.println("异常捕获后,代码可正常执行");
程序中可能出现的异常比较多,可以在catch后继续跟catch代码块
try {
Integer.parseInt("a");
System.out.println("出现异常");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("ArrayIndexOutOfBoundsException---");
}catch (NumberFormatException e){
System.out.println("NumberFormatException--");
}
System.out.println("异常捕获后,代码可正常执行");
可以使用Exception进行异常捕获,但是,如果其前有其他Exception子类的catch块,
则其子类需要在Exception前
try {
Integer.parseInt("a");
System.out.println("出现异常");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("ArrayIndexOutOfBoundsException---");
}catch (Exception e){
System.out.println("Exception--");
}
System.out.println("异常捕获后,代码可正常执行");
(2)try…catch…finally
相较于try...catch...代码块,多了最后的finally,其finally会在程序结束前或者
方法的return之前执行
static int getNum(){
try {
Integer.parseInt("a");
System.out.println("出现异常");
} catch (Exception e){
System.out.println("Exception--");
}finally {
System.out.println("finally");
}
return 0;
}
无论程序是否发生异常,均会执行finally中的代码
(3)throws
在程序中可能发生异常的地方比较多,在每一段代码中均进行代码的捕获处理,会使得相同
的代码比较多,不利于代码的维护,故可以使用throws关键字,对程序中的异常进行抛出,
将发生的异常交给调用的地方进行处理
static void function1() throws NumberFormatException {
Integer.parseInt("a");
}
static void function2() throws ArithmeticException {
System.out.println(10 / 0);
}
public static void main(String[] args) {
try {
function1();
function2();
}catch (Exception e){
System.out.println(e.getMessage());
}
}
可在一个方法中抛出多个异常,不同异常类间由“,”分割
(4)throw
可使用throw关键字在程序中手动抛出异常
static void function() throws NumberFormatException {
throw new NumberFormatException("抛出异常");
}
public static void main(String[] args) {
try {
function();
}catch (Exception e){
System.out.println(e.getMessage());
}
}
2.2.3 自定义异常类
除了java中已有的异常类外,还可以根据需要,自己定义异常类,自定义异常类需要继承
Throwable类或者其子类
public class MyException extends Exception{
@Override
public String getMessage() {
return "出错啦";
}
}
static void function() throws MyException {
throw new MyException();
}
public static void main(String[] args) {
try {
function();
}catch (Exception e){
System.out.println(e.getMessage());
}
}
可根据需要重写父类的方法
3.Error
Error为系统内部错误,一般不会遇到
while (true){
System.out.println(new Date());
}
如死循环会导致内存溢出或者栈溢出
笔记如有错误,遗漏之处,欢迎指正!