<NOSCRIPT><A href="http://about.com/" target=_blank><IMG src="C++ Tutorial - Lesson 34 Exceptions_files/0.gif" border=0></A></NOSCRIPT>   About > Computing & Technology > C/C++ 
Search     
 Hi, I'm John Kopp, your guide to C, C++ and C# programming. This site provides what you need to know to learn C, C++ or C# programming. Browse the subjects on the left, try one of the tutorials or use the search box to get started.

C/C++

with John Kopp
Your Guide to one of hundreds of sites
 Home · Articles · Forums · Chat · Newsletters · Help    
Subjects

  ESSENTIALS
· C Tutorial
· C++ Tutorial
· C/C++ Glossary
· Tips
· More Tutorials
  BUYER'S GUIDE

Product Reviews
Top Picks
C Tutorials
C++ Tutorials
C#
Advanced C
Advanced C++
Beginning C
Beginning C++
Books
C++ Builder
Careers
CGI
CM
Dictionaries
CompilingDebugging
Freeware/Shareware
Humor
Magazines
OOAD
Polls
SoftwareEngineer'n
Style
STL
UNIX/GNU
Visual C++

Subject Library

All articles on this topic

 

Stay up-to-date!
Subscribe to our newsletter.

Web Hosting
Global Servers

 
 
 
Advertisement
> Free Credit Report
 
C++ Tutorial - Lesson 34: Exceptions
 More of this Feature
• Introduction
• Exception Basics
• Exception Basics, Continued
• Using Class Objects
• Stack Class
• Analysis of Stack Class
• Stack Unwinding
 
 Related Resources
• Advanced C++ Tutorials
• Simple Solutions in C++
• C++ Programming Tips
• C++ Tutorial, All Lessons
 

by John Kopp

Introduction
Welcome to About.com's free tutorial on C++ programming. This lesson covers exceptions. Up to this point in the tutorial, we haven't worried much about errors or exceptions. First, let's distinguish between errors and exceptions. An error typically refers to a problem that exists in a program when it is written and compiled. It could be a logic error that will result in incorrect results. In this case, the code is correct but the algorithm has an error. This type of error will only be found during program testing or during design reviews of the program. Another type of error is a syntax error. Typically, the compiler finds errors of this type and they are corrected during the coding of the program. Exceptions are errors or anomalies that occur during the execution of a program. They can be the result of unavailability of system resources, such as memory, file space, channels or from data dependent conditions such as a divide by zero, or numerical overflow. Exceptions tend to be rare events but are predicable.

Given that exceptions are somewhat predicable, how should our programs handle them? Broadly, there are three types of responses we can take.

  • Not handle the exception. Allow the program to die or core dump.
  • Issue a warning. Print out some type of error message, probably at the spot in the code where the exception occurred and then exit.
  • Handle the exception gracefully and continue executing.
Certainly, the first way, doing nothing is not acceptable if you want to remain employed or pass your courses. The second way is a little better. Information about the exception is written out, but this is still not ideal. Most real world programs need to be more robust than this. Exceptions need to be handled and corrected. Execution must continue. Your mission to reach Mars can't fail due to a divide by zero.

Since we must handle exceptions, what features would assist us? Should the same section of code be raising the exception and handling the exception? Suppose an exception occurs in allocating memory. Should the function or method that attempted the allocation be the one to handle it? Can it? Probably some other, higher level, section of code will have the information necessary to decide how to handle the exception. Maybe different programs using the same classes and methods will handle exceptions differently. This points to a separation of the creation of an exception and its handling. The method in which an exception occurs could just alert its caller. This allows code that raises exceptions to be developed separately from code that handles them. If we pass exceptions up to calling routines, it is necessary to have a way to bundle information and for the exception to have some methods to assist in its handling.

The C++ exception mechanism handles both these features.

  • Exceptions may be raised and handled in different sections of code.
  • Any object, including class objects may be passes back to the handler of an exception. These objects can contain data and methods to assist in handling the exception.

Next page > Exception Basics > Page 1, 2, 3, 4, 5, 6, 7


Subscribe to the C/C++ Newsletter
Name
Email






Email this page | Sign up for a Newsletter |
Explore More
Most Popular Articles
• C++ Programming Tutorial
• C Programming Tutorial
• C++ Tutorial - Lesson 12: File Input and Output
• C++ Tutorial - Lesson 4: Input and Output - cin, cout, iostr...
• C++ Tutorial - Lesson 10: Arrays and Vectors
• C++ Tutorial - Lesson 1: Writing and Compiling A First Progr...
• Tutorials
What's Hot Now
• Teach Yourself C++ in 21 Days by Jesse Liberty
• C Programming Tutorial
• C++ Tutorial - Lesson 25: Operator Overloading
• The C Programming Language, 2nd Edition by Kernighan and Rit...
• Topics in C++: Function Template Specialization
• C Programming Tips: Using Long Constants
• C Tutorial - Lesson 5: Conditional Processing, Part 1 - Rela...
About Us | Advertise on This Site | User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2004 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.