public class ExceptionTest1
{
public static void main(String[] args)
{
try
{
YuanXing y = new YuanXing(5);
y.area(); 这儿报错了 Exception in thread "main" java.lang.Error: Unresolved compilation problem:
No enclosing instance of type ExceptionTest1 is accessible. Must qualify the allocation with an enclosing instance of type ExceptionTest1 (e.g. x.new A() where x is an instance of ExceptionTest1).
}
catch(NoValueException e)
{
System.out.println("输入非法值了");
}
}
}
class YuanXing
{
private int banjin;
public static final double PI = 3.14;
YuanXing(int banjin) throws NoValueException
{
if(banjin <= 0)
throw new NoValueException("输入非法值了");
this.banjin = banjin;
}
public void area()
{
System.out.println(banjin * banjin *PI);
}
}
class NoValueException extends Exception
{
NoValueException(String message)
{
super(message);
}
}
作者: 谭荣强 时间: 2014-7-7 17:42
验证代码没问题.....
public class Demo2
{
public static void main(String[] args)
{
try
{
YuanXing y = new YuanXing(-5);
y.area();
}
catch(NoValueException e)
{
System.out.println("输入非法值了");
}
}
}
class YuanXing
{
private int banjin;
public static final double PI = 3.14;
YuanXing(int banjin) throws NoValueException
{
if(banjin <= 0)
throw new NoValueException("输入非法值了");
this.banjin = banjin;
}
public void area()
{
System.out.println(banjin * banjin *PI);
}
}
class NoValueException extends Exception
{
NoValueException(String message)
{
super(message);
}
} 作者: Darkhorse′Xa 时间: 2014-7-7 17:54
试过了 是没有问题的!作者: mirror 时间: 2014-7-7 19:01