Java:枚举中的异常?

标签 java image exception enums

我正在尝试加载图像作为枚举的一部分,但它抛出异常,我找不到用 try catch 来解决它的方法,是否还有其他方法,或者不可能?

这里是一些注释代码,我正在使用 Slick2d 库。

    public enum Material {
//An enum of different materials, with different propierties and images

GRASS (5, false, null, true),

//Here I try to load an Image, but loading images throw an Exception, a Slick Exception I tried surronding it with try catch but its not working.
DIRT (5, false, new Image("res/Dirt.png"), true), 

WOOD (10, false, null, false), 

SNOW (1, false, null, false), 

ICE (1, false, null, false), 

LEAVES (1, false, null, false), 

STONE(15, false, null, false), 

COBBLESTONE(10, false, null, false), 

LOG(15, false, null, false),

AIR(1, false, null, false);


///variables
private int Resis;
private boolean traspasable;
private static Image I;
private boolean TreesCanGrow;

//getters
public int getResis(){
    return this.Resis;
}

public Image getTile(){
    return this.I;
}

public boolean isTraspasable(){

return this.traspasable;

}

public boolean getTreesCanGrow(){

return this.TreesCanGrow;

}


///constructor
private Material(int ExplosionResis, boolean isTraspasable, Image I, boolean TCG){  
this.Resis = ExplosionResis;
this.traspasable = isTraspasable;
this.TreesCanGrow = TCG;    
}


}

编辑: 新构造函数

///constructor
try {
    private Material(int ExplosionResis, boolean isTraspasable, String image, boolean TCG){ 
        this.Resis = ExplosionResis;
        this.traspasable = isTraspasable;
        this.TreesCanGrow = TCG;    
        this.I = new Image(image);
    }
}
catch (SlickException e) {
    e.printStackTrace();
}

}

最佳答案

我不建议将可能失败的代码放在枚举构造函数中或作为枚举值声明的参数。如果代码抛出异常,枚举类可能无法加载。

你可以尝试这样的事情:

/** An enum of different materials, with different properties and images. */
public enum Material {
  GRASS (5, false, null, true),

  DIRT (5, false, "res/Dirt.png", true), 

  ...

  AIR(1, false, null, false);


  private final int resis;
  private final boolean traspasable;
  private final String imagePath;
  private Image image;
  private final boolean treesCanGrow;

  private Material(int resis, boolean traspasable, String imagePath,
      boolean treesCanGrow) {  
    this.resis = resis;
    this.traspasable = traspasable;
    this.imagePath = imagePath;
    this.treesCanGrow = treesCanGrow;    
  }

  public synchronized Image getTile() throws SlickException {
    if (image == null && imagePath != null) {
      image = new Image(imagePath);
    }
    return image;
  }
}

但是,就我个人而言,我更喜欢枚举是不可变的,所以如果这是我的代码,我会将图像的创建放在其他地方(可能在延迟加载的 Map<Material, Image> 中)。

关于Java:枚举中的异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23177720/

相关文章:

java - 石头剪刀布游戏Java小程序

java - Android SQLite : get number of rows in a table?

java - 添加 DatePicker 后 ListView 消失

html - 带有自定义图像的样式单选按钮

c - 简单的 C 图像库?

javascript - 进度条问题(使用onloadstart、onloadend、onloadprogress)

java - 异常(exception)情况

java - 使用 Java 8 API 从 Set 到 Map

javascript - Babel 下调用 super() 自定义错误无法输出堆栈?

java - 使用递归错误。线程中的异常 "main"java.lang.StringIndexOutOfBoundsException : String index out of range: 0