public constructor (int x)
{
try
{
this.x = x;
if (x < 1 || x> MAX)
}
catch (IndexOutOfBoundsException e)
{
// array allocat
// calim error
}
}
如果我有接受需要在[1,MAX)int參數構造,我將如何正確地利用IndexOutOfBoundsException異常,如果參數是不在區間內?如果它是爲OBJ [] x數組分配內存的話。我以爲我可以只使用,如果其他人,但這是不一樣的行爲
if else else有什麼問題? – thedarkpassenger
你試過什麼代碼?另外,'IndexOutOfBoundsException'可能不是最好的選擇,你可能想使用['InvalidParameterException'](https://docs.oracle.com/javase/7/docs/api/java/security/InvalidParameterException。 HTML)。 – birryree
如果參數不在間隔內,你應該在構造函數 – AdamSkywalker