+ 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!

23rd May 2017, 1:56 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
10 Respuestas
+ 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
23rd May 2017, 2:26 PM
jay
jay - avatar
+ 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.
23rd May 2017, 2:08 PM
Ulisses Cruz
Ulisses Cruz - avatar
+ 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';
23rd May 2017, 2:15 PM
Ulisses Cruz
Ulisses Cruz - avatar
+ 4
By default it print to the console window like cout. But you can redirect it to where you want.
23rd May 2017, 2:18 PM
Ulisses Cruz
Ulisses Cruz - avatar
+ 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.
23rd May 2017, 2:37 PM
Ulisses Cruz
Ulisses Cruz - avatar
+ 4
@Ulisses Cruz and jay Thank you very much!
23rd May 2017, 2:38 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 3
@Ulisses Cruz Thank you very much, Sir...
23rd May 2017, 2:18 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 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?
23rd May 2017, 2:20 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 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?
23rd May 2017, 2:12 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 2
So, where does cerr write to? The same place as cout? Or places like build messages in Code::Blocks?
23rd May 2017, 2:17 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar