public constructor (int x)
{
try
{
this.x = x;
if (x < 1 || x> MAX)
}
catch (IndexOutOfBoundsException e)
{
// array allocat
// calim error
}
}
如果是,则为OBJ[] x数组分配内存。我认为我可以只使用if else,但这不是相同的行为
发布于 2016-02-01 04:50:10
为什么不用if else呢?
为什么你不能试一下这样的东西..
try{
//Your code
}catch(IndexOutOfBOundsException e){
if(){ // your 1st cond
}
else if(){// your 2nd cond
}
}
发布于 2016-02-01 18:01:52
我不明白为什么你需要一个异常,但是如果你想进入catch块,你必须抛出一个IndexOutOfBoundsException。然后,您可以在catch块中执行所需的操作。
try {
if (x < 1 || x > MAX)
throw new IndexOutOfBoundsException();
}
catch (IndexOutOfBoundsException e)
{
System.err.println("Index must be in 1 - MAX interval.");
e.printStackTrace();
//do what you want here
}
https://stackoverflow.com/questions/35119192
复制相似问题