Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用ArrayOutOfBoundException打印语句_Java_Oop_Exception_Indexoutofboundsexception - Fatal编程技术网

Java 如何使用ArrayOutOfBoundException打印语句

Java 如何使用ArrayOutOfBoundException打印语句,java,oop,exception,indexoutofboundsexception,Java,Oop,Exception,Indexoutofboundsexception,我正在尝试使用ArrayOutOfBoundException打印System.out.println语句,但我不太确定如何执行此操作 以下是我迄今为止所做的工作: public class Ex4 { public static void main(String[] args) { String text= ""; if ( args.length <= 3 ) { for (int i=0; i<args.le

我正在尝试使用
ArrayOutOfBoundException
打印
System.out.println
语句,但我不太确定如何执行此操作

以下是我迄今为止所做的工作:

public class Ex4
{
    public static void main(String[] args) {
        String text= "";

        if ( args.length <= 3 ) { 

            for (int i=0; i<args.length-1; i++) {
                text = text  + args[i];
            }

            System.out.println(text);
        }
        else if( args.length > 3 ) {
            throw new ArrayIndexOutOfBoundsException("Out of Bounds Exc. Size is 4 or more"); 
        }
    }
}
公共类Ex4
{
公共静态void main(字符串[]args){
字符串文本=”;

如果(args.length一个
ArrayIndexOutOfBoundsException
是一个
RuntimeException
,用户不应该真正看到它,更好的解决方案是直接打印到
System.err
或使用类似
log4j
的日志框架:

} else if (args.length > 3) {
    System.err.println("Out of Bounds Exc. Size is 4 or more"); 
}
但是,要回答您最初的问题,特别是要记录AIOOBE,您需要捕捉它:

try {

} catch (ArrayIndexOutOfBoundsException aioobe) {
    // log here...
}

但是,再次强调,捕获运行时异常并不是解决问题的方法,因为它们实际上是用来表示编程错误,而不是用户输入问题。一个
ArrayIndexOutOfBoundsException
是一个
运行时异常
,用户不应该真正看到它,更好的解决方案是直接打印到
System.err
或使用日志框架,如
log4j

} else if (args.length > 3) {
    System.err.println("Out of Bounds Exc. Size is 4 or more"); 
}
但是,要回答您最初的问题,特别是要记录AIOOBE,您需要捕捉它:

try {

} catch (ArrayIndexOutOfBoundsException aioobe) {
    // log here...
}

但是,再次强调,捕获运行时异常并不是解决问题的方法,因为它们实际上是用来表示编程错误,而不是用户输入问题。一个
ArrayIndexOutOfBoundsException
是一个
运行时异常
,用户不应该真正看到它,更好的解决方案是直接打印到
System.err
或使用日志框架,如
log4j

} else if (args.length > 3) {
    System.err.println("Out of Bounds Exc. Size is 4 or more"); 
}
但是,要回答您最初的问题,特别是要记录AIOOBE,您需要捕捉它:

try {

} catch (ArrayIndexOutOfBoundsException aioobe) {
    // log here...
}

但是,再次强调,捕获运行时异常并不是解决问题的方法,因为它们实际上是用来表示编程错误,而不是用户输入问题。一个
ArrayIndexOutOfBoundsException
是一个
运行时异常
,用户不应该真正看到它,更好的解决方案是直接打印到
System.err
或使用日志框架,如
log4j

} else if (args.length > 3) {
    System.err.println("Out of Bounds Exc. Size is 4 or more"); 
}
但是,要回答您最初的问题,特别是要记录AIOOBE,您需要捕捉它:

try {

} catch (ArrayIndexOutOfBoundsException aioobe) {
    // log here...
}

但是,再次强调,捕获运行时异常并不是解决问题的方法,因为它们实际上是用来表示编程错误而不是用户输入问题的

您想实现什么?如果您想调用
System.out.println
,只需这样做-为什么要抛出异常呢?这个问题来自我过去的一篇考试论文它要求使用这个例外来打印声明,否则我会做你提到的事情。感谢对我来说似乎是一个荒谬的问题。我建议你向你的导师(或其他人)提出这个问题为了检查你将要参加的任何考试是否可能包含这些奇怪的东西。你想实现什么?如果你想调用
System.out.println
,只需这样做-为什么你想抛出一个异常呢?这个问题来自我过去的一篇试卷,它要求使用该异常专门打印语句,否则我会照你说的做。谢谢对我来说似乎是个荒谬的问题。我建议你把这个问题告诉你的导师(或其他人)为了检查你将要参加的任何考试是否可能包含这些奇怪的东西。你想实现什么?如果你想调用
System.out.println
,只需这样做-为什么你想抛出一个异常呢?这个问题来自我过去的一篇试卷,它要求使用该异常专门打印语句,否则我会照你说的做。谢谢对我来说似乎是个荒谬的问题。我建议你把这个问题告诉你的导师(或其他人)为了检查你将要参加的任何考试是否可能包含这些奇怪的东西。你想实现什么?如果你想调用
System.out.println
,只需这样做-为什么你想抛出一个异常呢?这个问题来自我过去的一篇试卷,它要求使用该异常专门打印语句,否则我会照你说的做。谢谢对我来说似乎是一个荒谬的问题。我建议你把这个问题告诉你的导师(或其他人),看看你将要参加的任何考试是否会包括这些奇怪的东西。