什么是异常
java代码在运行时期发生的问题就是异常。在java中使用Exception来描述异常,throwable是Java 语言中所有错误或异常的超类,可以通过查看javase API查看
异常的处理
jvm默认处理方式
/*
* 异常的处理方式:
* jvm处理异常的方式:
* 如果出现异常我们没有处理,jvm会帮我们进行处理,他会把异常的类型,原因还有位置显示在命令行
* 并且还终止了程序,异常后面的代码将不在执行
*/
public class ExceptionDemo2 {
public static void main(String[] args) throws Exception {
System.out.println(2/0);
System.out.println("hello");
}
}
try…catch方式处理异常
try..catch的格式
try {
//需要被检测的语句。
}
catch(异常类 变量) { //参数。
//异常的处理语句。
}
finally {
//一定会被执行的语句。
// 通常是释放资源
}
try:该代码块中编写可能产生异常的代码。
catch:用来进行某种异常的捕获,实现对捕获到的异常进行处理。
/*
* 异常的处理方式:
* 捕获处理
* try...catch语句
*
* try {
* 有可能出现问题的代码;
* } catch(ArithmeticException ae) {
* 处理异常;
* }
*
* try...catch的执行顺序:
* 首先执行try语句
* 如果发现异常,异常下面的代码不在执行,直接跳入catch语句中,catch语句结束后,整个try...catch结束
* 如果没有发现异常,try语句执行结束后,try...catch直接结束, 不在执行catch语句
*
* jvm处理异常的方式:
* 如果出现异常我们没有处理,jvm会帮我们进行处理,他会把异常的类型,原因还有位置显示在命令行
* 并且还终止了程序,异常后面的代码将不在执行
*/
public class ExceptionDemo2 {
public static void main(String[] args) throws Exception {
try {
System.out.println(1);
//System.out.println(2 / 0);
System.out.println(2);
} catch(ArithmeticException ae) {
System.out.println("除数不能为0");
}
System.out.println(3);
}
}
throws方式处理异常
throws的使用方式:
权限修饰符 返回值类型 方法名(形参列表) throws 异常类型1,异常类型2….{
}
代码案例如下( 在方法上使用throws 关键字):
public class ExceptionDemo2 {
public static void main(String[] args) throws Exception {
//method();
function();
}
public static void function() throws Exception {
FileWriter fw = new FileWriter("a.txt");
}
private static void method() {
try {
System.out.println(1);
//System.out.println(2 / 0);
System.out.println(2);
} catch(ArithmeticException ae) {
System.out.println("除数不能为0");
}
System.out.println(3);
}
}
多异常的处理
处理格式:
void show(){ //不用throws
try{
throw new Exception();//产生异常,直接捕获处理
}catch(XxxException e){
//处理方式
}catch(YyyException e){
//处理方式
}catch(ZzzException e){
//处理方式
}
}
/*
注意:这种异常处理方式,要求多个catch中的异常不能相同,并且若catch中的多个异常之间有子父类异常的关系,那么子类异常要求在上面的catch处理,父类异常在下面的catch处理。
*/
代码案例如下:
/*
* 如何处理多个异常:
* 可以使用多个try...catch语句
* 使用一个try和多个catch
*
* 多个catch之间的顺序:
* 多个catch之间可以有子父类
* 平级之间没有顺序关系
* 如果有子父类,父类异常必须放在后面
*
*
*/
public class ExceptionDemo3 {
public static void main(String[] args) {
try {
String s = null;
System.out.println(s.length());
//int[] arr = new int[5];
//System.out.println(arr[8]);
//System.out.println(2 / 0);
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println("出现数组越界了");
}
catch(NullPointerException e) {
System.out.println("出现空指针了");
}
catch(Exception e) {
System.out.println("出现异常了");
}
/*try {
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("出现数组越界了");
}*/
}
private static void method() {
try {
String s = null;
System.out.println(s.length());
} catch(NullPointerException e) {
System.out.println("出现空指针了");
}
try {
int[] arr = new int[5];
System.out.println(arr[8]);
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("出现数组越界了");
}
}
}
catch 里面对应着异常的名称
一个io与异常结合的Demo(偏难,看不懂不必纠结)
import java.io.FileWriter;
import java.io.IOException;
/*
* finally:组合try...catch使用,用于释放资源等收尾工作,无论try...catch语句如何执行,finally的代码一定会执行
*
* try {
* 有可能出现问题的代码;
*
* } catch(异常对象) {
* 处理异常;
* } finally {
* 释放资源;
* 清理垃圾;
* }
*
*/
public class ExceptionDemo5 {
public static void main(String[] args) {
//method();
FileWriter fw = null;
try {
System.out.println(2 / 0);
fw = new FileWriter("a.txt");
fw.write("hello");
fw.write("world");
//System.out.println(2 / 0);
fw.write("java");
//fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
//释放资源
try {
if(fw != null) {
fw.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private static void method() {
try {
System.out.println(2 / 1);
} catch(ArithmeticException e) {
System.out.println("除数不能为0");
} finally {
System.out.println("清理垃圾");
}
}
}
编译时异常&运行时异常
* 编译时期异常:是Exception的子类,非RuntimeExcpetion的子类,在编译时期必须处理
* :RuntimeException和他的所有子类异常,都属于运行时期异常。NullPointerException,ArrayIndexOutOfBoundsException等都属于运行时期异常.
运行时期异常的特点:
方法中抛出运行时期异常,方法定义中无需throws声明,调用者也无需处理此异常
运行时期异常一旦发生,需要程序人员修改源代码.
代码案例:
import java.io.FileWriter;
import java.io.IOException;
/*
* 异常的分类:
运行时期异常:RuntimeException的子类就是运行时期异常,在编译时期可以自由选择处理或者不处理
编译时期异常:是Exception的子类,非RuntimeExcpetion的子类,在编译时期必须处理
*/
public class ExceptionDemo6 {
public static void main(String[] args) {
//System.out.println(2 / 0);
//String s = null;
//System.out.println(s.length());
try {
FileWriter fw = new FileWriter("a.txt");
} catch (IOException e) {
e.printStackTrace();
}
}
}
自定义异常
首先来个需求,按需处理异常
需求:写一个方法,接受考试成绩,如果考试成绩的范围在0-100之间则属于正常,否则属于异常
很明显,这事符合我们日常生活的一个,但是如果让java自行处理,给成绩赋值的时候,即便用short接受,都会超出,但是程序不会认为是错,因此,需要自定义异常
/* throws:处理异常的一种方式,把异常抛出,由调用者来处理
* throw:制造异常的方式,并且结束方法
*
* 注意:如果抛出(throw)的是编译时期异常,必须在方法声明处抛出(throws)
*
* 如何自定义一个异常类呢?
* 非常简单,写一个类去继承Exception或者RuntimeException,然后实现多个构造即可
*
* */
public class ExceptionDemo7 {
public static void main(String[] args) {
try {
checkScore(110);
} catch (Exception e) {
//System.out.println(e.getMessage());
e.printStackTrace();
}
//checkScore(110);
}
/*
普通的逻辑处理,
public static boolean checkScore(int score) {
//判断考试成绩是否符合范围,如果不符合则返回false
if(score < 0 || score > 100) {
return false;
}
//符合
return true;
}*/
// 使用异常的方式处理
public static void checkScore(int score) throws Exception {
if(score < 0 || score > 100) {
// 在此处使用throw关键字制造一个异常
throw new RuntimeException("考试成绩不符合要求");
//throw new Exception("考试成绩不符合要求");
}
System.out.println("考试成绩符合要求");
}
}
当然,我们也可以自定义一个异常,用来专门,处理这个情况下的特殊情况
/**
在继承Exception或者RuntimeException之后,重写父类的构造,
就可以完成一个自定义异常的定义,当出现异常的时候,传入指定的参数字符串,就可以完成指定功能的异常
*/
public class MyException extends /*RuntimeException*/ Exception{
public MyException() {
super();
}
public MyException(String message) {
super(message);
}
/*public MyException() {
super();
}
public MyException(String s) {
super(s);
}*/
}