import java.util.Scanner;
public class Exceptions {
public static void main(String[] args) {
// TODO Auto-generated method stub
B b=new B();
int a;
Scanner in = new Scanner(System.in);
System.out.print("请输入年龄: ");
a = in.nextInt();
try {
b.text(a);
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
class B {
public void text(int age) throws Exception{
if(age>0)
System.out.println(age);
else
throw new Exception("年龄在0-150之间");
}
}
//请输入年龄: -1
java.lang.Exception: 年龄在0-150之间
年龄在0-150之间
at B.text(Exceptions.java:28)
at Exceptions.main(Exceptions.java:13)
Exception 异常的根父类
throws Exception 抛出异常