package Exception;
public class ExceptionDemo2 {
// 运行时异常不需要try 不需要你处理了 需要你改代码
public static void main(String[] args) {
int[] a = { 1, 2, 3, -4, 5, 6, 7 };
int a1 = getAvg(a);
System.out.println(a1);
}
// 定义方法,抛自定义异常
public static int getAvg(int... arr) {
int sum = 0;
for (int i : arr)
{
if (i < 0)
{
throw new FuShuException("不能有负数" + i);
}
sum = sum + i;
}
return sum / arr.length;
}
}
// 自定义异常类继承自RuntimeException
class FuShuException extends RuntimeException {
public FuShuException(String a)
{
super(a);// 将参数继续传递给父类
}
public FuShuException()
{
}
}
