java中如何使用自定义异常?
发布网友
发布时间:2022-04-22 22:11
我来回答
共1个回答
热心网友
时间:2023-06-26 09:22
class NotScoreException extends Exception
{
public NotScoreException()
{
super();
}
public NotScoreException(String message)
{
super(message);
}
}
public class MyExceptionTest
{
public static String getGrade(double score)throws NotScoreException
{
if(score<=100&&score>=60)
{
return "及格";
}
else if(score<60&&score>=0)
{
return "不及格";
}
else
{
throw new NotScoreException("输入成绩有误");
}
}
public static void main(String[] args)
{
try
{
System.out.println("75分的等级为:"+getGrade(75));
System.out.println("105分的等级为:"+getGrade(105));
}
catch (NotScoreException e)
{
System.out.println("发生自定义异常,异常信息为:"+e.
getMessage
());
}
}
}