用java编写关于两数相加 当任意一个数为负数 抛出异常

如题所述

public class Test{
public static int add(int a,int b) throws Exception{
if(a<0||b<0)
throw new Exception();
return a+b;
}
public static void main(String[] args){
try{
System.out.println(add(2,3));
System.out.println(add(2,-1));
}catch(Exception e){
System.out.println("exception...");
}
}
}
温馨提示:答案为网友推荐,仅供参考
相似回答