+ 2
What is the difference between cerr and throw runtime_error from <exception>?
I have never used cerr, never, and so please forgive me if Im wrong. I have always used throw runtime_error to throw some allocation or custom option errors in the following way... if (!file.good()) throw runtime_error("File is no good..."); catch(runtime_error &e) cout<<e.what()<<endl; Please tell me, does cerr does the same thing? If yes, how is it different from throw runtime_error("");? And lastly, which can be a better option to use? Please help!
10 odpowiedzi
+ 5
I was reading an article about this on msdn. It says it is indeed an ostream object. It gets its error information from you. It is recommended that you use the std::exception class or one of the derived classes from the standard library. Or you derive your own from std::exception
The example they provide is as follows
MyData md;
try {
// code that could throw exception
md = GetNetworkResource();
}
catch (const networkIOExceptiom& e) {
// code that handles another exception type
cerr << e.what();
}
// throwing exception
MyData GetNetworkResource() {
if (IOSucess == false) {
throw networkIOException("Unable to connect");
if(readError) {
throw myDataFormatException("Format Error");
}
Hope this helps
+ 4
In your code you are catching the exception and then printing to cout, that by default is the console but can be redirected to a file, for example.
The cerr prints to standard error that by default is also the console but can be redirected too.
So, in you example you probably wanted to see the error in the console, using either would result in that.
But if you want your program to send the error to a log file, for example, and print the other informations to another file
then you can redirect each to its corresponding file.
Hope this helps you see the difference.
+ 4
Yes cerr is an ostream.
You can use it like you use cout:
cout << 'string send to cout\n';
cerr << 'string send to cerr\n';
+ 4
By default it print to the console window like cout. But you can redirect it to where you want.
+ 4
the difference:
cerr is an ostream. The programmer usually outputs the errors to it.
runtime_error is and exception. It is an object that describes the error that happened as the program was executing and the reason
it happened. This is the error. If you want to save the informations the object contain you can use cerr to output them to a file for example
for.
+ 4
@Ulisses Cruz and jay
Thank you very much!
+ 3
@Ulisses Cruz
Thank you very much, Sir...
+ 3
But similarly, e.what() can be written to any fstream object...
This again confuses me that what is the difference...
If cout and cerr write to the same place, is cerr able to detect errors?
+ 2
@Ulisses Cruz
So, cerr is just an ostream object?
Does it print errors in the build messages section?
Can you please give me an example, as I am not able to see how cerr gets the errors...
Can cerr get errors on its own?
Or one has to use if - else?
+ 2
So, where does cerr write to?
The same place as cout?
Or places like build messages in Code::Blocks?