第五章:异常、异常处理
一:Java中异常Throwable
(1)Error
代表程序在执行过程中出现错误,这种错误是程序员无法处理的,是不可修复的,由JVM进行处理
(2)Exception
代表程序在编译或者运行时出现的异常,是可以想办法解决的问题。我们程序员一般处理的就是由Exception派生出来的子类的异常
(3)Exception常见的两种类型
①:编译时异常
- 编译时异常:程序在编写的过程中出现的异常,我们需要处理才能继续运行程序
- 常见的编译时异常:文件未找到异常(FileNotFoundException)、类未找到异常(ClassNotFoundException)、IO异常(IOException)、未找到方法异常(NotSuchMethodException)
②:运行时异常
- 运行时异常:程序在运行时出现的异常,可处理可不处理
- 常见的异常有:1.算术异常(ArithmeticException)、2.空指针异常(NullPointerException)、3.数组下标越界异常ArrayIndexOutofBountsException、
4.数字格式化异常(NumberFormatException)、5.类抛出异常(ClassCastException)
二:异常的处理
(1) 三种处理异常的方法
①:try…catch…finally处理:
- try{}:试图处理,一般我们将可能出现异常的代码放到这个语句块中
- catch(){}:捕获异常,如果try的语句块出现异常,程序会在这里处理异常
- finally{}:不管程序是否出现错误,都会执行的语句块,这是一个善后的语句块,我们一般将资源关闭处理的操作放在这里。
- try…catch…finally处理的搭配方式有:try…carch() / try…finally /try…catch…finally三种搭配方式
- catch(){}语句块可以使用多个,但是要求捕获的异常其子类、具体的异常放前面,父类,通用的异常放后面,否则会报错
- 即是catch里有return 也会执行到finally语句块。
- return会将返回的值先储存起来,然后继续执行finally里的语句块,完成后才将储存的值返回
- 如果try代码块需要处理的代码出现异常,那么后面的代码也不会再执行
//3.处理异常第一种方法:try/..catch
private static void test03() {
try {
String str = null;
str.length();
int i = 10/0;
Class.forName("sada");
} catch (NullPointerException e) {//子类,具体的异常放在前面
System.out.println("空指针异常");
}catch (ArithmeticException e ){
System.out.println("算数异常");
}catch (Exception e ){ //父类、放在后面
System.out.println("出现其他异常");
}finally {
System.out.println("这里一般是对资源的关闭处理");
}
}
②:使用throws向上抛出异常(多用来调试程序):
- throws关键字代表向上抛出异常,抛出的异常是由其调用者来处理的。
- throws可以抛出多个异常,如果抛出的异常中有父子类关系,那么只抛出父类异常
- 此方法一般在练习中用,开发中一般使用throw或者try…catch
- 使用throws一般不可以自定义抛出异常的信息
public static void test02() throws IOException, ClassNotFoundException, SQLException {
InputStream is = new FileInputStream(new File("c:/abc.txt"));
Class.forName("xxxx");
is.read();
Connection connection = null;
PreparedStatement ps = connection.prepareStatement("select * from student");
}
③:throw抛出异常
- throw抛出异常,当条件发生时就会抛出异常,一般结合自定义异常使用
- throw 一般在判断语句块中,实现语句 throw new ArithmeticException(“错了!”)
public static void test03(){
int a = 0;
if( a == 0){
throw new ArithmeticException("不好意思了,出现了被零除异常!");
}else{
int i = 10 / a;
}
}
(2)自定义异常
- 可以继承RuntimeException 也可以继承 Exception。
- 继承RuntimeException 异常时:说明此异常时运行时异常,对调用此自定义异常的对象来说可处理可不处理
- 继承Exception异常时:说明此异常是编译时异常,必须处理
- 定义异常格式:创建一个自定义异常类------》继承异常类----》--------》定义私有属性-------》定义构造方法-------》定义访问属性的访问方法
/**
*
* 自定义异常类
*/
public class CustomException extends RuntimeException{
private String message;
@Override
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public CustomException(){
}
public CustomException(String message) {
this.message = message;
}
public static void test04(){
//4.1)定义用户名及密码
String username = "admin";
String password = "123";
//4.2)如果用户输入的用户名或密码不正确我们就抛出异常
Scanner scanner = new Scanner(System.in);
System.out.println("请输入用户名:");
String name = scanner.next();
System.out.println("请输入密码:");
String pwd = scanner.next();
//4.3)比较是否正确
if(!name.equals(username) || !pwd.equals(password)){
throw new CustomException("对不起,用户名或密码输入有误!");
}else{
System.out.println("欢迎学习Java异常处理!");
}
}