美文网首页
第九章:违例差错控制

第九章:违例差错控制

作者: pgydbh | 来源:发表于2018-04-21 10:46 被阅读22次

目录

1.说明&个人理解
2.违例使用1(try catch)
3.违例使用2(try throw catch)
4.自定义违例
5.看看try和finally

一:说明&个人理解

违例带来的好处

1.程序在处理中异常退出是最差的用户体验的一种。
2.违例处理,可以让我们把崩溃的信息转换为友好的用户提示,
3.违例,极大增强了程序的健壮性。

关于违例

throw& catch是一对(抛出&抓住)

二:违例使用1(try catch)

public class Six {
    public static void main(String[] args) {
        try{
            int a = 10;
            int b = 0;
            int c = a / b;
        }catch (ArithmeticException e){
            System.out.println("用户你好,你在进行除法操作的时候违法了!");
        }
    }
}

output:
用户你好,你在进行除法操作的时候违法了!

Process finished with exit code 

三:违例使用2(try throw catch)

public class Six {
    public static void main(String[] args) {
        try{
            int a = 10;
            int b = 0;
            int c = 0;
            if (b == 0)
                throw new ArithmeticException();
            else{
                System.out.println("测试是不是执行这里了");
                c = a / b;
            }
        }catch (ArithmeticException e){
            System.out.println("用户你好,你在进行除法操作的时候违法了!");
        }
    }
}

output:
用户你好,你在进行除法操作的时候违法了!

Process finished with exit code 0

四:自定义违例(try throw catch)

public class Six {
    public static void main(String[] args) {
        try{
            int a = 10;
            int b = 2;
            int c = 0;
            if (b == 2)
                throw new NumTwoException();
            else{
                System.out.println("测试是不是执行这里了");
                c = a / b;
            }
        }catch (NumTwoException e){
            System.out.println(e.getMessage());
        }
    }

    private static class NumTwoException extends Exception{
        @Override
        public String getMessage() {
            return "对不起这里不允许被除数字是2,i m sorry";
        }
    }
}

output:
对不起这里不允许被除数字是2,i m sorry

Process finished with exit code 0

五:看看try和finally

问题:如果try中有return,请问是先return还是先执行final中的内容?
1.如果finally中无return,先执行try中所有内容,之后执行finally中的内容,然后return try中的结果。函数结束。
2.如果finally中有return,先执行try中的所有内容,之后执行finally中内容,然后return finally中的结果。函数结束。
演示1
public class Six {

    private static String temp;
    public static void main(String[] args) {
        System.out.println("在main中执行:" + test());
    }

    public static String test() {
        try {
            return temp = "try";
        } catch (Exception e) {
            return temp = "catch";
        } finally {
            System.out.println("在finally中执行:" + temp);
        }
    }
}

output:
在finally中执行:try
在main中执行:try

Process finished with exit code 0
演示2
public class Six {

    private static String temp;
    public static void main(String[] args) {
        System.out.println("在main中执行:" + test());
    }

    public static String test() {
        try {
            return temp = "try";
        } catch (Exception e) {
            return temp = "catch";
        } finally {
            System.out.println("在finally中执行:" + temp);
            return "finally";
        }
    }
}

output:
在finally中执行:try
在main中执行:finally

Process finished with exit code 0

相关文章

网友评论

      本文标题:第九章:违例差错控制

      本文链接:https://www.haomeiwen.com/subject/ezntlftx.html