文章目录
异常体系结构
java.lang.Trowable
|-----java.lang.Error:一般不编写针对性的代码进行处理
|-----java.lang.Exception:可以进行异常的处理
|-----编译时异常(checked)
|-----IOException
|-----FileNotFoundException
|-----ClassNotFoundException
|-----运行时异常(unchecked)
|-----NullPointerException
|-----ArrayIndexOutOfBoundsException
|-----ClassCastException
|-----NumberFormatException
|-----InputMismatchException
|-----ArithmeticException
常见异常举例
public class ExceptionTest {
//******************以下是编译时异常***************************
@Test
public void test7() {
// File file = new File("hello.txt");
// FileInputStream fis = new FileInputStream(file);
//
// int data = fis.read();
// while(data != -1){
// System.out.print((char)data);
// data = fis.read();
// }
//
// fis.close();
}
//******************以下是运行时异常***************************
//ArithmeticException
@Test
public void test6() {
int a = 10;
int b = 0;
System.out.println(a / b);
}
//InputMismatchException
@Test
public void test5() {
Scanner scanner = new Scanner(System.in);
int score = scanner.nextInt();
System.out.println(score);
scanner.close();
}
//NumberFormatException
@Test
public void test4() {
String str = "123";
str = "abc";
int num = Integer.parseInt(str);
}
//ClassCastException
@Test
public void test3() {
Object obj = new Date();
String str = (String) obj;
}
//IndexOutOfBoundsException
@Test
public void test2() {
//ArrayIndexOutOfBoundsException
// int[] arr = new int[10];
// System.out.println(arr[10]);
//StringIndexOutOfBoundsException
String str = "abc";
System.out.println(str.charAt(3));
}
//NullPointerException
@Test
public void test1() {
// int[] arr = null;
// System.out.println(arr[3]);
String str = "abc";
str = null;
System.out.println(str.charAt(0));
}
}
如何处理异常
- 方式一:try-catch-finally
- 方式二:throws+异常类型
异常的处理:抓抛模型
过程一:“抛”:程序在正常执行过程中,一旦出现异常,就会在代码出生成一个对应异常类的对象,一旦抛出对象以后,其后的代码就不再执行。
过程二:“抓”:可以理解为异常的处理方式:① try-catch-finally ② throws
try-catch-finally的使用
- finally是可选的
- 使用try将可能刚出现的异常代码包装起来,在执行过程中,一旦出现异常,就会生成一个对应异常类的对象,根据此对象的类型,去catch中进行匹配
- 一旦try中的异常匹配到某一个catch时,就进入catch中进行异常的处理。一旦处理完成,就跳出当前的try-catch结构,继续执行其后的代码。
- catch中异常类型如果没有子父类关系,则声明的上下顺序没有关系,如果有子父类关系,那么要求,子类一定声明在父类的上面,否则报错。
- 常用的异常处理方式:① String getMessage() ② printStackTrace()
- 在try中声明的变量,除了try结构就不可以再使用。
- finally内的代码是一定会被执行的代码,并且在return之前执行
- try-catch-finally结构是可以嵌套的
- 体会:一般使用try-catch处理编译时异常,一般不处理运行时异常
finally嵌套结构
public class FinallyTest {
@Test
public void test2(){
FileInputStream fis = null;
try {
File file = new File("hello1.txt");
fis = new FileInputStream(file);
int data = fis.read();
while(data != -1){
System.out.print((char)data);
data = fis.read();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(fis != null)
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void testMethod(){
int num = method();
System.out.println(num);
}
public int method(){
try{
int[] arr = new int[10];
System.out.println(arr[10]);
return 1;
}catch(ArrayIndexOutOfBoundsException e){
e.printStackTrace();
return 2;
}finally{
System.out.println("我一定会被执行");
return 3;
}
}
@Test
public void test1(){
try{
int a = 10;
int b = 0;
System.out.println(a / b);
}catch(ArithmeticException e){
e.printStackTrace();
// int[] arr = new int[10];
// System.out.println(arr[10]);
}catch(Exception e){
e.printStackTrace();
}
// System.out.println("我好帅啊!!!~~");
finally{
System.out.println("我好帅啊~~");
}
}
}
异常处理方式二:throws+异常类型
throws + 异常类型 写在方法的声明处。指名此方法执行时,可能会抛出异常类型。一旦当方法体执行时,出现异常,仍会在异常代码处生成一个异常类的对象,此对象满足thows后异常类型时,就会被抛出(谁调用就抛给谁)。异常代码后续的代码,就不再执行!
体会1: try-catch-finally:真正将异常处理掉了。
体会2:hrows的方式只是将以后抛给了方法的调用者。并没有真正将异常处理掉。
开发中如何选择使用try-catch-finally 还是使用throws?
- 如果父类中被重写的方法没有throws方式处理异常,则子类重写的方法也不能使用throws,意味着如果子类重写的方法中有异常,必须使用try-catch-finally方式处理。
- 执行的方法a中,先后又调用了另外的几个方法,这几个方法是递进关系执行的。我们建议这几个方法使用throws的方式进行处理。而执行的方法a可以考虑使用try-catch-finally方式进行处理。
手动抛出异常
- throw new Exception :需要直接处理,不处理会报错
- throw new RuntimeException(“输入的信息有误”):可以先不处理,运行时再报错处理
自定义异常
- 继承与现有的异常结构,RuntimeException,Exception
- 提供全局常量:serialVersionUID
- 提供重载的构造器
- 使用:throw new MyException(“不能输入复数”);
public class MyException extends RuntimeException{
static final long SerialVersionUID = -11111111L;
public MyException(){
}
public MyException(String msg){
super(msg);
}