How to display an error message in c++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to display an error message in c++

19th Apr 2017, 12:03 PM
Harsh Vardhan
Harsh Vardhan - avatar
4 Answers
+ 8
@Aniket Dhanak I suggest you study a bit 'more' about C++ before making such comments...
19th Apr 2017, 12:52 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 8
@Harsh Vardhan So how do you want to show the error? Using cout in the terminal or printing a standard error in the build messages?
19th Apr 2017, 12:53 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 2
You simply print an according message to the type of error. You can use if statements to find about an "error" that occures in your code (can be anything you choose it to be) and you can also use the try/catch blocks if an exception has occured (an exception is a type of error that occures in runtime and will crash your program if unhandled e.g divide by zero exception). The way to use the try/catch block is as following: try { //Here the code that can cause an exception should be written } catch(exception& e) { //Here the code which handles the exception should be written //In the brackets of the catch statement you can put any kind of exception you'd like to and even strings, ints etc... //You can use multiple catch blocks to catch different kind of exception (exception kind is the superclass of all exceptions therefore any exception that occurs will enter a catch block that catches an exception type). //The block who's exception has been caught first will be the only one to execute from all of the catch blocks //You can also use (...) in the catch block to catch anything (exceptions, strings etc...) //You can creat your own exception by making a class that inherits from the exception class //Every exception has a function called "what" that returns a string which describes the exception //You can throw your own exceptions (or strings etc...) by using the "throw" keyword } Note that exceptions require more resources to use therefore you should use ifs when you can.
19th Apr 2017, 12:59 PM
G4S
0
You can either use cerr to write in the console or create a log file to output the error details. To identify the error, use try - throw - catch.
19th Apr 2017, 8:53 PM
Denis Felipe
Denis Felipe - avatar