-1

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

  1. 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);
}
  1. The command middleLine.add(lines[middleIndex]); throws an exception. Since Java is quite idiomatic, does the Java keyword throws implie the exception is not automatically sent to the standard error stream or does throws indicate the exception's motion in the standard error output stream of data?
  2. Is the Exception thrown by the command middleLine.add(lines[middleIndex]); caught before it gets to the standard error stream?
  3. 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

  1. Does the excerpt from the javadoc for err mean that the command System.err.println(e) sends the Exception e that was caught using the catch command to the standard error stream? Then because the destination of the standard error stream is the display screen by default, the method println sends the error output data to the standard error stream where it is sent to the display screen automatically?

  2. 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 using System.err.println(e) the message is displayed on the computer screen because the destination of the standard error stream is the display screen

  3. If the catch command catches the error in the standard error output stream is the error message no longer in the error stream?
  4. 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?
  5. 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 method println?
bit
  • 443
  • 1
  • 7
  • 19
  • 2
    You are supposed to ask one question per question, not 9 or 10. You also have a ton of misconceptions. When you call println, then you actually print e.toString(),and that string simply goes to whatever console you sent it to. When you don't do anything, the jvm prints the exception message and stack trace to system.err (aka stderr) and stops. – GhostCat Jul 14 '19 at 19:57
  • 2
    And no, throwing and catching doesn't print anything by default. – GhostCat Jul 14 '19 at 19:58
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/196481/discussion-between-slaw-and-mywrathacademia). – Slaw Jul 15 '19 at 16:39
  • @Slaw the chat room you created seems to have been deleted. Is it possible to get those links you provided in the chat room? – bit Aug 06 '19 at 18:51
  • [`PrintStream`](https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/io/PrintStream.html) | [`Throwable#printStackTrace()`](https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/lang/Throwable.html#printStackTrace()) | [§11.3 (JLS)](https://docs.oracle.com/javase/specs/jls/se12/html/jls-11.html#jls-11.3) | [Standard streams - Wikipedia](https://en.wikipedia.org/wiki/Standard_streams) | [Thread (computing) - Wikipedia](https://en.wikipedia.org/wiki/Thread_(computing)) | [What is a “thread” (really)?](https://stackoverflow.com/questions/5201852/). – Slaw Aug 29 '19 at 11:45

1 Answers1

2

An exception will only print to stderr if it is not caught, or you call printStackTrace() on it. Here's a little demo that should demonstrate some things:

public class foo {

    public static void main(String[] args) {
        System.out.println("This prints to stdout");
        System.err.println("This prints to stderr");
        int x;
        try {
            x = 1 / 0;
        } catch (ArithmeticException e) {
            System.err.println("Printing stack trace to stderr");
            e.printStackTrace();
            System.err.println("Printing message to stderr");
            System.err.println(e.getMessage());
        }
        try {
            x = 1 / 0;
        } catch (ArithmeticException e) {
            // nothing sent to stderr
        }
        System.out.println("Un-caught exception will kill program and print stack trace to stderr");
        x = 1 / 0;
    }
}

...

Erics-MacBook-Pro:tmp redekopp$ javac foo.java
Erics-MacBook-Pro:tmp redekopp$ java foo > foo-stdout.txt 2> foo-stderr.txt
Erics-MacBook-Pro:tmp redekopp$ cat foo-stderr.txt
This prints to stderr
Printing stack trace to stderr
java.lang.ArithmeticException: / by zero
at foo.main(foo.java:8)
Printing message to stderr
/ by zero
Exception in thread "main" java.lang.ArithmeticException: / by zero
at foo.main(foo.java:16)
Erics-MacBook-Pro:tmp redekopp$ cat foo-stdout.txt
This prints to stdout
Un-caught exception will kill program and print stack trace to stderr
eRedekopp
  • 63
  • 7
  • Which of his questions are you answering? – Akaisteph7 Jul 14 '19 at 21:06
  • #1, and 7,8,9 to a lesser extent : meant to demonstrate that nothing gets sent to stderr until it's explicitly told to print or if it runs into an uncaught exception. – eRedekopp Jul 14 '19 at 21:16
  • Just to confirm, when you say "prints to stdout" and "prints to stderr", you mean writes data to stdout and writes data to stderr, right? Your correct that an exception prints to stderr if it's not caught, which means catching an exception intercepts the exception thereby preventing stderr from writing the exception from the program to the display [this image explains standard streams](https://en.wikipedia.org/wiki/Standard_streams#/media/File:Stdstreams-notitle.svg). Just to add another, I believe declaring that a method throws an exception also stops stderr from writing data to the display – bit Jul 15 '19 at 16:11