try catch finally

来源:互联网 发布:刚开的淘宝店没生意怎么办 编辑:程序博客网 时间:2024/06/05 18:52
class Age {    public void setAge(int age) throws Exception{        if (age < 12 && age > 0){            System.out.println("This is a child");        }else if(age >= 18){            System.out.println("This is a adult");        }else if(age >=12 && age < 18){            System.out.println("This is a youth");        }else{            Exception e = new Exception("The age can't be negative number");            throw e;        }    }}public class TestDemo {    public static void main(String args[]){        Age a = new Age();        try{            a.setAge(-1);        }        catch(Exception e){            System.out.println(e);        }        finally {            System.out.println("The End");        }    }}
0 0