Errors in C++
Learning to code can be a frustrating endeavor because you are destined to encounter many red errors along the way. What makes a programmer successful isn’t avoiding errors — no programmer can avoid them. Great programmers understand that errors are part of the process, and they know how to find the solution to each while learning something new from them. In this article, we’ll teach you how to think about errors in your code a little differently.
“Errors are simply unavoidable when you develop a program, yet the final program must be free of errors.”
There are many ways of classifying errors. For example:
- Compile-time errors: Errors found by the compiler. We can further classify compile-time errors based on which language rules they violate, for example:
- Syntax errors
- Type errors
- Link-time errors: Errors found by the linker when it is trying to combine object files into an executable program.
- Run-time errors: Errors found by checks in a running program. We can further classify run-time errors.
- Logic errors: Errors found by the programmer looking for the causes of erroneous results.
As you recall, the programming process looks like:
The errors above occur at these places:
[Image coming soon!]
Compile-time errors:####
When you are writing C++ programs, your compiler is your first line of defense against errors. Here are the two types of compile-time errors:
Syntax errors:
int x =6// error: missing ;Int x =6;// error: Int is not a type
Type errors:
int x ="hello";// error: wrong type
Run-time errors:
If your program has no compile-time errors and no link-time erorrs, it’ll run. This is where the fun really starts.
Logic errors:
Once we have removed the initial compiler and liner errors, the program runs. Sometimes, no output is produced or that the output that the program produces is just wrong. This can occur for a number of reasons. Maybe your understanding of the underlying program is flawed; maybe you didn’t write what you though you wrote.
'The Codecademy Team, composed of experienced educators and tech experts, is dedicated to making tech skills accessible to all. We empower learners worldwide with expert-reviewed content that develops and enhances the technical skills needed to advance and succeed in their careers.'
Meet the full teamRelated articles
- Article
Transforming Code Errors into Learning Opportunities
Discover how to view coding errors as valuable learning experiences in our comprehensive guide. Learn effective strategies for identifying, analyzing, and fixing errors in your code. - Article
Exception & Error Handling in Python
Learn how to handle Python exceptions using try-except blocks, avoid crashes, and manage errors efficiently. Explore Python error-handling techniques, including built-in exceptions, custom exceptions, and best practices. - Article
Handle Errors Using Throwing Functions in Swift
Learn how to handle errors in Swift with `do-catch` blocks and write and call throwing functions.
Learn more on Codecademy
- Free course
Learn JavaScript: Error Handling
Learn how to create more secure applications by predicting, handling, and debugging errors with JavaScript.Beginner Friendly1 hour - Free course
Learn Java: Debugging
Learn about different types of errors in Java and practice finding them.Beginner Friendly< 1 hour - Free course
Learn C: Conditionals and Loops
Level up your programming skills by learning how to use C loops and conditionals like `if` and `else`.Beginner Friendly2 hours