
public class NumException extends RuntimeException{}//创建NumException类 自定义异常public class Test4 {//测试类 public static void main(String[] args) { System.out.println(add(1)); } public static int add(Object o){ if(!(o instanceof Integer)){ //判断传入参数是否为整数类型 throw new NumException(); //抛出异常 }else { int num (int) o; return num; } } }