求解java中在什么情况下需要用到自定义异常
1个回答
2016-11-09
展开全部
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());
}
}
}
{
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());
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询