Java:
Writing to standard error
How to:
Basic stderr Output in Java
Java provides a straightforward way to write to stderr using System.err.print()
or System.err.println()
. Here’s how you do it:
public class StdErrExample {
public static void main(String[] args) {
try {
int division = 10 / 0;
} catch (ArithmeticException e) {
System.err.println("Error: Cannot divide by zero.");
}
}
}
Sample output:
Error: Cannot divide by zero.
This will directly print the error message to the standard error stream.
Using a Logger for Advanced Error Handling
For applications that need more sophisticated error handling and logging, using a logging library like SLF4J with Logback or Log4J2 is common. This allows for more flexibility in managing error output, including file redirection, filtering, and formatting.
Example with Logback
First, add the dependency for Logback to your pom.xml
(Maven) or build.gradle
(Gradle) file. For Maven:
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
Then, you can use the following code to log errors:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LoggerExample {
private static final Logger logger = LoggerFactory.getLogger(LoggerExample.class);
public static void main(String[] args) {
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
logger.error("Error: Cannot divide by zero.", e);
}
}
}
This will output the error message along with a stack trace to the console or a file, depending on the Logback configuration.
Using logging frameworks like Logback provides more control over error handling, making it easier to manage large applications and systems.