Java(26):异常

package yichang;

/*
 * https://www.cnblogs.com/hysum/p/7112011.html
 * */

public class Demo1 {

    public static void main(String[] args) {
        
        Demo1 t1 = new Demo1();
        System.out.println(t1.test1());
        
    }
    
    public int test1() {
        int divider = 10;
        int result = 100;
        
        try {
            while (divider > -1) {
                divider--;
                result = result + 100/divider;
            }
            return result;
        }catch(Exception e) {
            e.printStackTrace();
            System.out.println("异常抛出!");
            return -1;
        }
    }
}
package yichang;

public class Demo2 {

    public int test2() {
        int divider = 10;
        int result = 100;
        
        try {
            while(divider > -1) {
                divider--;
                result = result + 100/divider;
            }
            return result;
        }catch(Exception e) {
            e.printStackTrace();
            System.out.println("异常抛出!");
            return result = 999;
        }finally {
            System.out.println("finally");
            System.out.println("result = "+result);
        }
    }
    
    public static void main(String[] args) {
        
        Demo2 t2 = new Demo2();
        t2.test2();
        System.out.println("end.");
        
    }
    
}
package yichang;

public class Demo3 {

    public static void main(String[] args) {
        
        String s = "abc";
        if (s.equals("abc")) {
            throw new NumberFormatException();
        }else {
            System.out.println(s);
        }
        
        System.out.println("main end.");
    }
    
}
package yichang;

public class Demo4 {
    
    public static void function() throws NumberFormatException {
        String s = "abc";
        System.out.println(Double.parseDouble(s));
    }

    public static void main(String[] args) {
        
        try {
            function();
        }catch(NumberFormatException e) {
            System.out.println("非数据类型不能转换");
        }
        
        System.out.println("main end.");
    }
    
}
package yichang;

/*
 * 自定义异常抛出信息
 * 
 * */

public class Demo5 extends Exception {

    private String errorCode;
    
    public Demo5() {
        System.out.println("demo5()...");
    }
    public Demo5(String message) {
        super(message);
        System.out.println("demo5(String)...");
    }
    
    public String getErrorCode() {
        return errorCode;
    }
    
    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }
    
    public static void main(String[] args) {
        
        String[] sexs = {"male", "female", "neutral"};
        for(int i=0; i<sexs.length; i++) {
            if ("neutral".equals(sexs[i])) {
                try {
                    throw new Demo5("不存在中性人");
                }catch(Demo5 e) {
                    e.printStackTrace();
                }
            }else {
                System.out.println(sexs[i]);
            }
        }
    }
}
package yichang;

public class Demo6 {

    public void test1() throws RuntimeException {
        String[] sexs = {"male", "female", "neutral"};
        for (int i=0; i<sexs.length; i++) {
            if ("neutral".equals(sexs[i])) {
                try {
                    throw new Demo5("不存在中性人");
                }catch(Demo5 e) {
                    e.printStackTrace();
                    RuntimeException rte = new RuntimeException(e);
                    throw rte;
                }
            }else {
                System.out.println(sexs[i]);
            }
        }
        
    }
    
    public static void main(String[] args) {
        
        Demo6 m = new Demo6();
        
        try {
            m.test1();
        }catch(Exception e) {
            e.printStackTrace();
            e.getCause();
        }
        
    }
    
}
原文地址:https://www.cnblogs.com/kenantongxue/p/14118362.html