使用throw关键字会把一个异常类抛出,转到异常处理程序去处理。但是如果throw后面的语句没有错误,如何在处理完毕之后回到throw后面继续执行后面正确的代码?
class ExceptionDemo1
{
public void in(int )
{
try
{
if(>100)throw new Exception("分数太高");
if(<0)throw new Exception("分数太低");
System.out.println("分数是:"+);
}
catch(Exception e)
{
System.out.println("您输入了错误的分数。");
}
}
public static void main(String args[])
{
ExceptionDemo1 aa=new ExceptionDemo1();
aa.in(30);
aa.in(200);
aa.in(89);
System.out.println("程序执行完毕,退出.");
}
}
如果真的不能继续执行后面的代码,那还要throw干吗?干脆if...else算了。不如写成
if(>100)
System.out.println("输入的分数太高");
else if(<0)
System.out.println("输入的分数太低");
else
System.out.println("您输入的分数是"+);
class ExceptionDemo1
{
public void in(int )
{
try
{
if(>100)throw new Exception("分数太高");
if(<0)throw new Exception("分数太低");
System.out.println("分数是:"+);
}
catch(Exception e)
{
System.out.println("您输入了错误的分数。");
}
}
public static void main(String args[])
{
ExceptionDemo1 aa=new ExceptionDemo1();
aa.in(30);
aa.in(200);
aa.in(89);
System.out.println("程序执行完毕,退出.");
}
}
如果真的不能继续执行后面的代码,那还要throw干吗?干脆if...else算了。不如写成
if(>100)
System.out.println("输入的分数太高");
else if(<0)
System.out.println("输入的分数太低");
else
System.out.println("您输入的分数是"+);
aa.in(89);
正确输出.
你这的 in 不该用异常的。