关于Java自定义异常类。定义MyException继承Exception。 20
1个回答
展开全部
//创建继承父类的自定义异常类MyException:
package bao.ming;
public class MyException extends Exception{
public MyException(String name)
{
super(name);
}
}
//创建公共类NewCustomException类:
package bao.ming;
import bao.ming.MyException;
public class NewCustomException {
public static double avg(int x,int y) throws MyException {
double avg=(double)(x+y)/2;
if(x>=100||x<=0|| y>=100||y<=0){
throw new MyException("my exception:输入参数要在100以内的正整数");
}
return avg;
}
public static void main(String args[]){
double avg=0;
try {
avg=avg(90,99);
System.out.println(avg);
} catch (MyException e) {
e.printStackTrace();
}
}
}
//这位兄弟,我可是弄了挺久才弄出来的,一定要哦!
package bao.ming;
public class MyException extends Exception{
public MyException(String name)
{
super(name);
}
}
//创建公共类NewCustomException类:
package bao.ming;
import bao.ming.MyException;
public class NewCustomException {
public static double avg(int x,int y) throws MyException {
double avg=(double)(x+y)/2;
if(x>=100||x<=0|| y>=100||y<=0){
throw new MyException("my exception:输入参数要在100以内的正整数");
}
return avg;
}
public static void main(String args[]){
double avg=0;
try {
avg=avg(90,99);
System.out.println(avg);
} catch (MyException e) {
e.printStackTrace();
}
}
}
//这位兄弟,我可是弄了挺久才弄出来的,一定要哦!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询