C program to display even number Program Method 1 c program to display even number up to 20 using for loop.
-->In modern C++, in most scenarios, the preferred way to report and handle both logic errors and runtime errors is to use exceptions. This is especially true when the stack might contain several function calls between the function that detects the error and the function that has the context to know how to handle it. Exceptions provide a formal, well-defined way for code that detects errors to pass the information up the call stack.
Program errors are generally divided into two categories: logic errors that are caused by programming mistakes, for example, an 'index out of range' error, and runtime errors that are beyond the control of programmer, for example, a 'network service unavailable' error. In C-style programming and in COM, error reporting is managed either by returning a value that represents an error code or a status code for a particular function, or by setting a global variable that the caller may optionally retrieve after every function call to see whether errors were reported. For example, COM programming uses the HRESULT return value to communicate errors to the caller, and the Win32 API has the GetLastError function to retrieve the last error that was reported by the call stack. In both of these cases, it's up to the caller to recognize the code and respond to it appropriately. If the caller doesn't explicitly handle the error code, the program might crash without warning, or continue to execute with bad data and produce incorrect results.
Exceptions are preferred in modern C++ for the following reasons:
An exception forces calling code to recognize an error condition and handle it. Unhandled exceptions stop program execution.
An exception jumps to the point in the call stack that can handle the error. Intermediate functions can let the exception propagate. They do not have to coordinate with other layers.
The exception stack-unwinding mechanism destroys all objects in scope according to well-defined rules after an exception is thrown.
An exception enables a clean separation between the code that detects the error and the code that handles the error.
The following simplified example shows the necessary syntax for throwing and catching exceptions in C++.
Exceptions in C++ resemble those in languages such as C# and Java. In the try block, if an exception is thrown it will be caught by the first associated catch block whose type matches that of the exception. In other words, execution jumps from the throw statement to the catch statement. If no usable catch block is found, std::terminate
is invoked and the program exits. In C++, any type may be thrown; however, we recommend that you throw a type that derives directly or indirectly from std::exception
. In the previous example, the exception type, invalid_argument, is defined in the standard library in the <stdexcept> header file. C++ does not provide, and does not require, a finally block to make sure that all resources are released if an exception is thrown. The resource acquisition is initialization (RAII) idiom, which uses smart pointers, provides the required functionality for resource cleanup. For more information, see How to: Design for Exception Safety. For information about the C++ stack-unwinding mechanism, see Exceptions and Stack Unwinding.
Basic guidelines
Robust error handling is challenging in any programming language. Although exceptions provide several features that support good error handling, they can't do all the work for you. To realize the benefits of the exception mechanism, keep exceptions in mind as you design your code.
Use asserts to check for errors that should never occur. Use exceptions to check for errors that might occur, for example, errors in input validation on parameters of public functions. For more information, see the section titled Exceptions vs. Assertions.
Use exceptions when the code that handles the error might be separated from the code that detects the error by one or more intervening function calls. Consider whether to use error codes instead in performance-critical loops when code that handles the error is tightly-coupled to the code that detects it.
For every function that might throw or propagate an exception, provide one of the three exception guarantees: the strong guarantee, the basic guarantee, or the nothrow (noexcept) guarantee. For more information, see How to: Design for Exception Safety.
Throw exceptions by value, catch them by reference. Don’t catch what you can't handle.
Don't use exception specifications, which are deprecated in C++11. For more information, see the section titled Exception specifications and noexcept.
Use standard library exception types when they apply. Derive custom exception types from the exception Class hierarchy.
Don't allow exceptions to escape from destructors or memory-deallocation functions.
Exceptions and performance
The exception mechanism has a very minimal performance cost if no exception is thrown. If an exception is thrown, the cost of the stack traversal and unwinding is roughly comparable to the cost of a function call. Additional data structures are required to track the call stack after a try block is entered, and additional instructions are required to unwind the stack if an exception is thrown. However, in most scenarios, the cost in performance and memory footprint is not significant. The adverse effect of exceptions on performance is likely to be significant only on very memory-constrained systems, or in performance-critical loops where an error is likely to occur regularly and the code to handle it is tightly coupled to the code that reports it. In any case, it's impossible to know the actual cost of exceptions without profiling and measuring. Even in those rare cases when the cost is significant, you can weigh it against the increased correctness, easier maintainability, and other advantages that are provided by a well-designed exception policy.
Exceptions vs. assertions
Exceptions and asserts are two distinct mechanisms for detecting run-time errors in a program. Use asserts to test for conditions during development that should never be true if all your code is correct. There is no point in handling such an error by using an exception because the error indicates that something in the code has to be fixed, and doesn't represent a condition that the program has to recover from at run time. An assert stops execution at the statement so that you can inspect the program state in the debugger; an exception continues execution from the first appropriate catch handler. Use exceptions to check error conditions that might occur at run time even if your code is correct, for example, 'file not found' or 'out of memory.' You might want to recover from these conditions, even if the recovery just outputs a message to a log and ends the program. Always check arguments to public functions by using exceptions. Even if your function is error-free, you might not have complete control over arguments that a user might pass to it.
C++ exceptions versus Windows SEH exceptions
Both C and C++ programs can use the structured exception handling (SEH) mechanism in the Windows operating system. The concepts in SEH resemble those in C++ exceptions, except that SEH uses the __try, __except, and __finally constructs instead of try and catch. In the Microsoft C++ compiler (MSVC), C++ exceptions are implemented for SEH. However, when you write C++ code, use the C++ exception syntax.
For more information about SEH, see Structured Exception Handling (C/C++).
Exception specifications and noexcept
Exception specifications were introduced in C++ as a way to specify the exceptions that a function might throw. However, exception specifications proved problematic in practice, and are deprecated in the C++11 draft standard. We recommend that you do not use exception specifications except for throw()
, which indicates that the function allows no exceptions to escape. If you must use exception specifications of the type throw(
type)
, be aware that MSVC departs from the standard in certain ways. For more information, see Exception Specifications (throw). The noexcept
specifier is introduced in C++11 as the preferred alternative to throw()
.
See also
How to: Interface Between Exceptional and Non-Exceptional Code
C++ Language Reference
C++ Standard Library
The articles in this section of the documentation explain diagnostic error and warning messages that are generated by the Microsoft C/C++ compiler and build tools.
Important
The Visual Studio compilers and build tools can report many kinds of errors and warnings. After an error or warning is found, the build tools may make assumptions about code intent and attempt to continue, so that more issues can be reported at the same time. If the tools make the wrong assumption, later errors or warnings may not apply to your project. When you correct issues in your project, always start with the first error or warning that's reported, and rebuild often. One fix may make many subsequent errors go away.
To get help on a particular diagnostic message in Visual Studio, select it in the Output window and press the F1 key. Visual Studio opens the documentation page for that error, if one exists. You can also use the search tool above to find articles about specific errors or warnings. Or, browse the list of errors and warnings by tool and type in the navigation pane on this page.
Note
Not every Visual Studio error or warning is documented. In many cases, the diagnostic message provides all of the information that's available. If you landed on this page when you used F1 and you think the error or warning message needs additional explanation, let us know. You can use the feedback buttons on this page to raise a documentation issue on GitHub, or a product issue on the Developer Community site. You can also send feedback and enter bugs within the IDE. In Visual Studio, go to the menu bar and choose Help > Send Feedback > Report a Problem, or submit a suggestion by using Help > Send Feedback > Send a Suggestion.
You may find additional assistance for errors and warnings in Microsoft's public forums. Or, search for the error or warning number on the Visual Studio C++ Developer Community site. You can also search for errors and warnings and ask questions on Stack Overflow to find solutions.
For links to additional help and community resources, see Visual C++ Help and Community.
In this section
BSCMAKE errors and warnings (BKxxxx)
Errors and warnings generated by the Microsoft Browse Information Maintenance Utility (BSCMAKE.EXE).
Command-line errors and warnings
Errors and warnings generated by the build tools for command-line options issues.
Compiler fatal errors C999 - C1999
Errors that halt the C++ compiler (CL.EXE).
Compiler errors C2001 - C3999
Errors detected by the C++ compiler (CL.EXE).
Compiler warnings C4000 - C5999
Warnings for issues detected by the C++ compiler (CL.EXE).
Compiler warnings by compiler version
A list of the warnings introduced by each compiler version.
C Runtime errors (Rxxxx)
Errors generated at runtime by the C Runtime Library (CRT).
CVTRES errors and warnings (CVTxxxx)
Errors and warnings generated by the Microsoft Resource File To COFF Object Conversion Utility (CVTRES.EXE).
Expression evaluator errors (CXXxxxx)
Errors generated by the debugger and diagnostics tools.
Linker tools errors and warnings (LNKxxxx)
Errors and warnings generated by the linker and related tools (LINK.EXE, LIB.EXE, DUMPBIN.EXE, EDITBIN.EXE).
Math errors (Mxxxx)
Errors generated by the runtime floating-point math library.
NMAKE errors and warnings (Uxxxx)
Errors and warnings generated by the Microsoft makefile tool (NMAKE.EXE).
Profile-Guided Optimization errors and warnings (PGxxxx)
Errors and warnings generated by the Profile-Guided Optimization (PGO) tools.
Project build errors and warnings (PRJxxxx)
Errors and warnings generated by the native C++ Project build system in Visual Studio.
Resource compiler errors and warnings (RCxxxx, RWxxxx)
Errors and warnings generated by the Resource Compiler (RC.EXE).
Vectorizer and parallelizer messages
Diagnostic messages generated by the vectorizer and parallelizer optimization compiler options.
How To Display Error Messages Dev-c Windows 10
Related sections
See also
Error Message Generator
C/C++ Building Reference
Debugging in Visual Studio