一道简单的JAVA练习题关于异常的
来源:百度知道 编辑:UC知道 时间:2024/06/09 00:16:42
以下是我编的:
public class LessThanZeroException extends Exception
{
public LessThanZeroException(String s)
{
super(s);
}
public LessThanZeroException()
{
this("");
}
}
class Mult1
{
protected double result;
public Mult1(double result)throws LessThanZeroException
{
this.set(result);
}
public void set(double result) throws LessThanZeroException
{
if(result<0)
throw new LessThanZeroException(""+result);
}
public static void main(String args[])
{
double m=4;
double n=0.2;
double result=m*n;
try
{
????????
}
catch(LessThanZeroException e)
{
System.out.println("产生乘积小于0异常!");
}
finally
{
System.ou
public Mult1(double result)throws LessThanZeroException
没有返回值,至少应该定义个void
另外main()方法应该放在public的类里吧
修改后的代码(没用到Mult1()方法)
class LessThanZeroException extends Exception
{
public LessThanZeroException(String s)
{
super(s);
}
public LessThanZeroException()
{
this("");
}
}
public class Mult1
{
protected double result;
public void Mult1(double result)throws LessThanZeroException
{
this.set(result);
}
public void set(double result) throws LessThanZeroException
{
if(result<0)
throw new LessThanZeroException(""+result);
}
public static void main(String args[])
{
double m=4;
double n=0.2;
double result=m*n;
try
{
if(result<0) {
throw new LessThanZeroException();
}
}
catch(LessThanZeroException e)
{
System.out.println("产生乘积小于0异常!