In Unix-like operating systems, standard error is the destination of error messages from command line programs and the default destination of standard error is the display screen. And I'm assuming that just like in Unix-like operating systems each command in Java is assigned three data streams (i.e. channels): standard input, standard output and standard error
- Is the same true in Java? That is, in Java is an error message from a command sent to the standard error stream instantly or does an error message from a command have no destination (i.e. the error message from a command does not go anywhere unless we catch it in a variable then send it to the standard error stream using
System.err.println(e);
)?
Using the following piece of code as an example:
ArrayList<String> middleLine = new ArrayList<String>();
int beginIndex = 0;
int endIndex = list.size() - 1;
int middleIndex = (beginIndex + endIndex) / 2;
try {
middleLine.add(lines[middleIndex]);
} catch(Exception e) {
System.err.println(e);
}
- The command
middleLine.add(lines[middleIndex]);
throws an exception. Since Java is quite idiomatic, does the Java keywordthrows
implie the exception is not automatically sent to the standard error stream or doesthrows
indicate the exception's motion in the standard error output stream of data? - Is the Exception thrown by the command
middleLine.add(lines[middleIndex]);
caught before it gets to the standard error stream? - Or is the Exception thrown by the command
middleLine.add(lines[middleIndex]);
caught when it is already in the standard error stream?
The javadoc for the static variable err
says:
The "standard" error output stream. This stream is already open and ready to accept output data
Does the excerpt from the javadoc for
err
mean that the commandSystem.err.println(e)
sends the Exception e that was caught using thecatch
command to the standard error stream? Then because the destination of the standard error stream is the display screen by default, the methodprintln
sends the error output data to the standard error stream where it is sent to the display screen automatically?Do we have to catch the error message (i.e. the exception) in order for the error message to be sent to the standard error output stream when we give it as a parameter to the method
println
of the standard error PrintStream object? Meaning that when we print the error message that was sent to the standard error output stream usingSystem.err.println(e)
the message is displayed on the computer screen because the destination of the standard error stream is the display screen- If the
catch
command catches the error in the standard error output stream is the error message no longer in the error stream? - If the
catch
command catches the error message before the error message enters the standard error stream is the error message still sent to the standard error stream? - Does
System.err.println(e)
mean print the exception e that matches the exception that was sent to the standard error stream automatically or does it mean print the exception e that we are sending to the standard error stream via the parameter of the methodprintln
?