拓冰建站拓冰建站
首页 / 资讯中心 / 正文

定义一个方法,对传入的参数进行++操作,同时要求参数必须是整数类型,如果传入的参数不是整数类型则抛出自定义异常

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; } } }
分享:

看完干货,该让你的企业上线了

免费需求沟通 · 48 小时内出具建站方案 · 河南本地可上门