try-catch-finally

主讲人:王少华  QQ群号:483773664

学习目标

1、掌握try-catch-finally

2、掌握try-catch-finally分别跟System.exit(1)和return一起使用注意事项

一、finally执行流程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class Test2 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
            System.out.print("请输入被除数:");      
        try {
            int num1 = in.nextInt();
            System.out.print("请输入除数:");
            int num2 = in.nextInt();
            System.out.println(String.format("%d / %d = %d",
                    num1, num2, num1/ num2));
            System.out.println("感谢使用本程序!");
        }catch(InputMismatchException ie){
            System.out.println("输入的类型不匹配");
            ie.printStackTrace();
        }catch (ArithmeticException e) {
            System.err.println("除数不能为零");
            e.printStackTrace();
        }
        catch (Exception e) {
            System.err.println("出现错误:被除数和除数必须是整数," +
                    "除数不能为零。");
            e.printStackTrace();
        }
    }
}

如果想让上面的代码,无论是否发生异常,都执行输出“感谢使用本程序!”语句。该怎么实现

在try-catch语句块加入finally块,把该语句放入finally块。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class Test4 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("请输入被除数:");
        try {
            int num1 = in.nextInt();
            System.out.print("请输入除数:");
            int num2 = in.nextInt();
            System.out.println(String.format("%d / %d = %d", num1, num2, num1
                    / num2));
        } catch (InputMismatchException ie) {
            System.out.println("输入的类型不匹配");
            ie.printStackTrace();
        } catch (ArithmeticException e) {
            System.err.println("除数不能为零");
            e.printStackTrace();
        } catch (Exception e) {
            System.err.println("出现错误:被除数和除数必须是整数," + "除数不能为零。");
            e.printStackTrace();
        } finally {
            System.out.println("感谢您使用本程序!");
        }
    }
}

测试



二、try-catch-finally使用注意事项

(一)finally语句唯一不被执行的情况:异常代码块中执行:System.exit(1);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class Test4 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("请输入被除数:");
        try {
            int num1 = in.nextInt();
            System.out.print("请输入除数:");
            int num2 = in.nextInt();
            System.out.println(String.format("%d / %d = %d", num1, num2, num1
                    / num2));
        } catch (InputMismatchException ie) {
            System.out.println("输入的类型不匹配");
            ie.printStackTrace();
        } catch (ArithmeticException e) {
            System.err.println("除数不能为零");
            e.printStackTrace();
            System.exit(1);
        } catch (Exception e) {
            System.err.println("出现错误:被除数和除数必须是整数," + "除数不能为零。");
            e.printStackTrace();
        } finally {
            System.out.println("感谢您使用本程序!");
        }
    }
}

(二)try-catch-finally结构

try块是必须的,catch和finally块为可选,但两者必须出现其中之一

(三)try中或catch中出现return

发生异常执行的顺序:执行try块或catch中return之前的语句,执行finally块中语句,执行try块或catch中的return语句退出。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class Cal {
    public String division(String stra, String strb) {
        try {
            int a = Integer.parseInt(stra);
            int b = Integer.parseInt(strb);
            int c = a / b;
            return String.valueOf(c);
        } catch (NumberFormatException e) {
            System.err.println("数字格式异常:程序只能接受整数参数");
            return "数字格式异常";
        } catch (ArithmeticException e) {
            System.err.println("算术异常:除数不能为零");
            return "算术异常";
        } catch (Exception e) {
            System.err.println("未知异常");
            return "未知异常";
        }finally{
            System.out.println("感谢您使用本程序!"

0

收藏

imentors

105篇文章,9W+人气,0粉丝

Ctrl+Enter 发布

发布

取消

0

分享
imentors