在程序2中引用程序1中自定义的类出现标题那种错误。
简单代码如下:
程序1:
package sharp;
public class 1{
  class a extends Exception{}
  class b extends a{}
  class c extends b{}
  }程序2:
package sharp;
import sharp.1.*;
public class 2{
  if()
    throw new c();
  }
程序2的throw new c();这条语句就会出现错误:
an enclosing instance that contains sharp.1.c is required
请问这个问题怎么解决,谢谢。

解决方案 »

  1.   

    public class 2{
    if()
    throw new c();
    }
    这是什么写法。。?
      

  2.   

    简写,if里的条件判断没有写,意思就是条件不符合就抛出c这个异常类。
      

  3.   

    package temp;public class w {
    class a extends Exception{}
    class b extends a{}
    class c extends b{}
    }
    package temp;import temp.w.c;public class q {

    public q() throws Exception {
    if(1!=1){
    throw new c();
    }
    }



    public void exp() throws Exception {
    if(1!=1){
    throw new c();
    }
    }
    }
    我是说你把那段代码写在类的那个位子是什么意思,我上面这样在构造函数里或者是别的函数里抛出异常都是可以的
      

  4.   

    我明白你说的意思了,我初学,只想描述问题所以这样简单写了,你说的那段代码的位置是在别的函数里的。public void exp() throws Exception {
    if(1!=1){
    throw new c();
    }
    这个位置的。你的可以啊。
    但我的ide就提示那个问题。ide是netbeans5。5
      

  5.   

    在网上查了一下,
    创建非static内部类的实例时,需要使用外围类的一个实例来使用new语法
    例如 throw (new 1()).new c();或者将a、b、c都声明为static
      

  6.   

    我那个写错了。。白金说的是对的,需要new w().new c();我那个写成1!=1,ide没提示,改成1==1就提示出来了。。