异常——try、catch、finally、throw、throws

异常(exception)就是程序在执行过程中发生的、破坏程序正常指令流的事件。

异常是对象,对象都要用类来定义。异常的根类是java.lang.Throwable。

 

常见异常:

ArrayIndexOutOfBoundsException  访问数组越界

InputMismatchException  输入数据类型与声明不匹配

FileNotFoundException  找不到指定文件

ArithmeticException  数学运算异常

详细参考博客:https://blog.csdn.net/yuwenlanleng/article/details/84646448

抛出异常有三种形式:①系统自动抛出 throw throws 

①系统自动抛出

 1   import java.util.Scanner;
 2   public class Test {
 3   
 4       public static void main(String[] args) {
 5           Scanner input =new Scanner(System.in);
 6           System.out.println("请输入被除数:");
 7                  try {                     
 8                        int num1=input.nextInt();
 9                        System.out.println("请输入除数:");
10                        int num2=input.nextInt();
11                        System.out.println(String.format("%d / %d = %d",num1, num2, num1 / num2));
12                   }catch (Exception e) {
13                       System.err.println("出现错误:被除数和除数必须是整数,"+"除数不能为零。");
14                       System.out.println(e.getMessage());
15                   }
16      }
17  }

运行结果:

System.err.println:输出错误信息,控制台呈红色。

当除数为0时,系统抛出异常,然后转而调用catch,catch捕捉到了异常进入catch内部。


 

throw

 1 import java.util.Scanner;
 2 public class Test {
 3     public static int quotient(int number1,int number2) {
 4         if(number1==0) 
 5             throw new ArithmeticException("除数不能为零") ;
 6         return number1 /number2;
 7     }
 8 
 9     public static void main(String[] args) {
10         Scanner input =new Scanner(System.in);
11         System.out.println("请输入被除数:");
12                 try {                 
13                     int num1=input.nextInt();
14                     System.out.println("请输入除数:");
15                     int num2=input.nextInt();
16                     int result=quotient(num1,num2);
17                     System.out.println(String.format("%d / %d = %d",num1, num2, result));
18                  }catch (ArithmeticException e) {
19                      System.err.println("出现错误:被除数和除数必须是整数,"+"除数不能为零。");
20                      System.out.println(e.getMessage());
21                  }     
22                 System.out.println("继续...");
23     }
24 }

运行结果:

 

方法quotient来抛出异常,该异常可以被调用者捕获和处理。

throw语句的执行称为抛出一个异常,异常类是java.lang.ArithmeticException。

当异常被抛出,正常的执行流程就被中断,throw相当于调用catch块,如果类型匹配则执行执行catch块,执行完后不反回到throw语句,而是执行catch块后的下一语句。


throws 

throws就是声明一系列可能抛出的异常,多个异常逗号分开。

1 public static int quotient(int number1,int number2) throws ArithmeticException{
2         if(number1==0) 
3             throw new ArithmeticException("除数不能为零") ;
4         return number1 /number2;
5     }

抛出多个异常的情况

 1 import java.util.InputMismatchException;
 2 import java.util.Scanner;
 3 
 4 public class Test {
 5 
 6     public static void main(String[] args) {
 7         Scanner input =new Scanner(System.in);
 8         System.out.println("请输入被除数:");
 9                 try {                 
10                     int num1=input.nextInt();
11                     System.out.println("请输入除数:");
12                     int num2=input.nextInt();
13                     System.out.println(String.format("%d / %d = %d",num1, num2, num1/num2));
14                  }catch (ArithmeticException e) {
15                      System.err.println("出现错误:"+"除数不能为零。");
16                      System.out.println(e.getMessage());
17                  }catch (InputMismatchException e) {
18                      System.err.println("出现错误:被除数和除数必须是整数,");
19                 }
20                 System.out.println("继续...");
21     }
22 }

运行结果:


finally

不论异常是否产生,finally子句总是会被执行。

猜你喜欢

转载自www.cnblogs.com/codercql/p/13394862.html