+ 1
How to display an error message in c++
4 Respuestas
+ 8
@Aniket Dhanak
I suggest you study a bit 'more' about C++ before making such comments...
+ 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?
+ 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.
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.