Java 手动抛出异常
我如何在Java中手动抛出一个Java 手动抛出异常,java,Java,我如何在Java中手动抛出一个IndexOutOfBoundsException,并可选地打印一条消息?您只需: throw new IndexOutOfBoundsException("your message goes here"); 如果需要打印该消息,请从捕获异常的位置打印。(您可以使用getMessage()方法访问消息。)如下所示: throw new IndexOutOfBoundsException("If you want a message, put it here");
IndexOutOfBoundsException
,并可选地打印一条消息?您只需:
throw new IndexOutOfBoundsException("your message goes here");
如果需要打印该消息,请从捕获异常的位置打印。(您可以使用getMessage()
方法访问消息。)如下所示:
throw new IndexOutOfBoundsException("If you want a message, put it here");
这实际上并不打印消息;它只是在准备。要打印邮件,请执行以下操作:
try {
//...
throw new IndexOutOfBoundsException("If you want a message, put it here");
} catch (IndexOutOfBoundsException e) {
System.out.println(e.getMessage());
}
将来,我建议在发布之前四处寻找答案。您可以使用throw语句抛出异常。throw语句需要一个参数:throwable对象。Throwable对象是Throwable类的任何子类的实例。下面是一个throw语句的示例
throw someThrowableObject;
例如:
public void example() {
try{
throw new IndexOutOfBoundsException();
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
}
}
你试过什么?什么不起作用?你到底有什么困难?什么,没有
int[]arr=newint[10];arr[10]代码>?:P@Kublai汗,我试着在一个类似的问题上发表这一点,结果被否决了。是的,我知道-我是被否决的选民之一(幽默/橄榄枝)@Kublai Khan LOL,我刚刚得到了P