Javatpoint Logo
Javatpoint Logo

Cerr in C++

Various streams are available in the C++ standard library for handling input and output activities. One of these streams is called cerr, which is short for "standard error". Cerr is made specifically for error messages and diagnostics, unlike the cout stream, which is used for general output. In this article, we'll investigate cerr's features, go over its syntax and C++ usage and give code samples with their related results.

The standard error device is connected to the cerr stream, which is commonly the console or terminal. During the execution of a C++ program, it is mostly used to output error messages, warnings, and other diagnostic data. Each character or message is displayed on the output device without delay by default because cerr is not buffered.

You must include the <iostream> header file in your C++ program to use cerr, which gives access to C++'s standard input/output capability. Here is an illustration of how to use the required headers:

The operator can be used to write messages to the cerr stream in a manner similar to how it would be used with the cout stream. The following is the syntax for writing to cerr:

Syntax:

Here, message is the error message or diagnostic data you want to display, and std::cerr stands for the standard error stream. Let's look at an illustration:

Example:

Output:

An error occurred!

Explanation:

In the above example, the operator is used to write the error message "An error occurred!" to the cerr stream. The console immediately shows the message.

Similar tocout, cerr allows you to prepare output using a variety of manipulators given by the iomanip header. For instance, you can alter the output's width and alignment, as well as the precision of floating-point figures. Here is an illustration of formatting in cerr:

Example:

Output:

The value of pi is: 3.1416

Using std::setprecision(4) and std::fixed, the output precision was set to 4 decimal places in the code. It ensures the desired level of precision when displaying the value of pi.

Using rdbuf() function

The console is connected by default to the cerr stream, which is typically the standard error device. The cerr command can be redirected to a file to record error messages and diagnostics. You can accomplish this by utilizing the rdbuf() function and the file stream (<fstream>). Here's an illustration:

Example:

Output:

The error

The following message will appear in the log file "error.log":

An error occurred!

Explanation:

We construct an ofstream object called errorLog and link it to the file "error.log" in the code. After that, the cerr stream is forwarded to the errorLog stream buffer using the rdbuf() method. Therefore, any messages sent to cerr will be sent to the designated log file rather than the terminal.

The processing and reporting of errors is one of cerr's most prevalent use cases. You can use cerr to give the user helpful information about an error that happens while a program is running. You can make sure that error messages are visible and distinct from regular program output by exporting them to cerr. Here's an illustration:

Example:

Output:

Error: Division by zero!

Explanation:

In this demonstration, we try to divide 10 by the variable "divisor" starting value, which is set to zero. Since division by zero is prohibited, if the divisor is 0, we send an error message to cerr.

Conclusion

In this blog post, we looked at the C++ cerr stream, which is used as the standard error stream for diagnostics and error messages. We discussed its syntax and usage with code samples showcasing cerr's capabilities. Additionally, we went over formatting the output and rerouting cerr to a file. Using cerr correctly may improve error handling and give users useful information while running your C++ programs.


Next TopicMake_shared in C++





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA