Getting Started with C++

Error Messages

Error messages are programming errors whose detection does not occur until the program is compiled or executed. Furthermore, it is certainly mandatory to remove errors before compiling and executing. Moreover, the various types of errors are syntax errors, linker errors, logical errors, semantic errors, and runtime errors.

Error Messages

Syntax Errors

Syntax errors take place when a violation of the rules of writing C/C++ syntax takes place. Furthermore, this compiler error is an indication to fix something before the compiling of the code. Also, the most frequent syntax errors are missing semicolon, printing the value of variable without declaring it, and missing parenthesis.

Missing semicolon error is demonstrated below:

                    

// C program to illustrate

// syntax error

#include

void main()

{

int x = 10;

int y = 15;

printf("%d", (x, y)) // semicolon missed

}

Error:

error: expected ';' before '}' token

Syntax of a basic construct is written wrong. For example : while loop

                    

// C program to illustrate

// syntax error

#include

int main(void)

{

// while() cannot contain "." as an argument.

while(.)

{

printf("hello");

}

return 0;

}

Error:

error: expected expression before '.' token

while(.)

In the given example, the syntax of while loop is incorrect. This causes a syntax error.

Browse more Topics Under Getting Started with C++

Linker Errors

These types of error messages occur when after compilation, the linking of the different object files takes place with main’s object using Ctrl+F9 key(RUN). Furthermore, the generation of these error messages takes place when the executable of the program cannot be generated. One of the common linker error messages is writing Main() instead of main().

                    

// C program to illustrate

// linker error

#include

void Main() // Here Main() should be main()

{

int a = 10;

printf("%d", a);

}

Error:

(.text+0x20): undefined reference to `main'

Logical Errors

Logical errors provide output that appears to be error-free but is actually incorrect. Furthermore, these logical errors arise most commonly with the beginners of programming. This is one of the common error messages.

                    

// C program to illustrate

// logical error

int main()

{

int i = 0;



// logical error : a semicolon after loop

for(i = 0; i < 3; i++);

{

printf("loop ");

continue;

}

getchar();

return 0;

}

No output

Semantic Errors

The occurrence of this error happens when the statements written in the program do not turn out to be meaningful to the compiler.

                    

// C program to illustrate

// semantic error

void main()

{

int a, b, c;

a + b = c; //semantic error

}

Error

error: lvalue required as left operand of assignment

a + b = c; //semantic error

Runtime Errors

Runtime errors are those that happen during program execution(run-time) after successful compilation. Division by zero error, also known as Division error, is a common run-time error.
See the example below of a division by zero error:

                    

// C program to illustrate

// run-time error

#include

void main()

{

int n = 9, div = 0;



// wrong logic

// number is divided by 0,

// so this program abnormally terminates

div = n/0;



printf("resut = %d", div);

}

Error:

warning: division by zero [-Wdiv-by-zero]

div = n/0;

FAQs For Error Messages

Question 1: What is the way of returning an error in C++?

Answer 1: When it comes to error messages, below are the three options of returning an error in C++:

  •  Create a possible error code and a class containing the return value.
  • Use something which allows for invalid responses like boost::optional for the return value.
  • Pass a reference to a variable. Afterwards, returning any possible error code that would be within that.

Question 2: How can one handle errors in C++?

Answer 2: When it comes to error messages, one can handle errors in C++in the following ways:

  • throw – Whenever a problem arises, a program throws an exception.
  • catch − A program catches an exception that is characterized by an exception handler. Moreover, this happens at the place in a program where one intends to handle the problem.
  • try − A try block identifies a block of code for which the activation of the particular exceptions will take place.
Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.