C++

Topic: Exceptions

How many exceptions in a List-Based Stack ?

A more involved example is provided by revising our old list-based stack program to use exception handling.    itemtype.h    listnode.h header file that sets up ListNodeClass    listnode.cpp    list.h header file that sets up ListClass    list.cpp modified to use exceptions    stack.h header file that sets up the abstract base class for stacks    lststack.h header file that sets up the derived class for list-based stacks    lststack.cpp modified to use exceptions    reverse.cpp Let's look first at the list.cpp file. In particular, let's look at the GetNode function. Part of the code for this function is repeated below:NodePtr = new ListNodeClass(Item, NextPtr);if (NodePtr == NULL)   {   throw "Cannot allocate memory";   }Note that the value of the exception is a literal string. It is stored as a character array. Remember that an array name is seen as a pointer to the first character in the array. Thus, the type of the exception is seen as char *.Warning: Check your compiler to see how the new operation behaves when it cannot allocate space. According to the most recent C++ standard as of this writing, it should throw an exception of type bad_alloc, but Visual C++ 5.0, the compiler used to check our examples, instead just returns a value of NULL, which is what many older compilers do. This compiler supposedly has a way to tell new to use exceptions instead, but even then it does not match the current standard since the type of the exception is xalloc.In the same file, look at the ListClass constructor. It allocates a dummy node for the front of the list. If an exception of type char * is raised when trying to do this, an error message is printed that contains the (string) value of the exception and the program is exited. Of course, the string printed is the literal string used when the exception is thrown by GetNode: "Cannot allocate memory". The choice to exit the program was made because it seemed to be a rather serious error if we cannot even allocate space for the dummy node of a list.The InsertFront function uses GetNode, as shown below, to create a new node holding the data to be inserted. (The InsertRear and InsertInOrder functions also do this.) If an exception of type char * is caught, then we print an error message containing the (string) value of the exception and re-throw the exception. Re-throwing the exception is accomplished by the throw statement below. Whatever called InsertFront should try to catch this exception and take appropriate action. (If not, the program will terminate.)try   {   NodePtr = GetNode(Item, Front->Next);   }catch(char * Str)   {   cout << "Error:  Could not insert at front -- " << Str      << endl;   throw;   }The RemoveFront function checks the count of how many items are on the list before trying to remove. If that number is zero, it throws an exception. Note that this one is an integer exception. The relevant section of code follows:if (Count == 0)   {   throw Count;   }Next, look at the lststack.cpp file. It uses InsertFront to implement the Push operation, and so needs to check if InsertFront has thrown an exception. If a char * exception is raised, Push prints an error message. However, we have chosen not to exit the program, thinking that perhaps this error is not fatal and that the user might want to continue in spite of the error.In the same file you see the Pop function. It checks for an integer exception, since that is the type that might be raised by the RemoveFront function that is called by Pop. If such an exception is caught, a warning message is printed.We could also have chosen to re-throw the exception if either Push or Pop catches an exception. Then the reverse.cpp application program would need to contain code to catch such exceptions, much like we did in the above array-based stack program. It seemed to be sufficient to let it go as is. 

Browse random answers:

What is Exceptions in c++ ?
Can we generate a C++ source code from the binary file?
what is Un handled exception(KERNEL32.DLL):0xE06D7363 in the context of exception handling? pls answer to my mail id.in the flg code i got a problem.i want to know the entire steps followed in exception handling in this program.#include <iostream>using namespace std;int main(){ cout << "Start"; try  { // start a try block cout << "Inside try block"; cout << "Still inside try block"; throw 78; } catch (double i)  { // catch an error cout << "Caught an exception -- value is: "; cout << i << ""; } cout << "End"; return 0;}
How does throwing and catching exceptions differ from using setjmp and longjmp?
what is the difference betwen wait() and delay()?
How can I get around scope problems in a try/catch?
Explain exception specifications with example ?
Explain about standard exceptions with example ?
Explain about try, catch, and throw Statements (C++) ?
Exceptions are hard to ignore, unlike error codes. ?
What are C++ Exception  Examples ?
Explain about Unhandled C++ Exceptions ?
Explain about exception Handling Overhead ?
Explain about Mixing C (Structured) and C++ Exceptions ?
Explain about Overview of C++ Exception Handling with example ?
Explain about Implications of Using Exceptions with example ?
Explain about Designing With Exceptions with example ?
Explain about exceptions in Real-Time Systems ?
Explain about exception Handling Philosophies with example ?
Explain about exceptions in an Array-Based Stack ?
How many exceptions in a List-Based Stack ?
What are some ways try / catch / throw can improve software quality? 
How do exceptions simplify my function return type and parameter types? 
What does it mean that exceptions separate the "good path" (or "happy path") from the "bad path"? 
Exception handling seems to make my life more difficult; clearly I'm not the problem, am I?? 
How can I handle a destructor that fails? 
How can I handle a constructor that fails? 
How should I handle resources if my constructors may throw exceptions? 
What is exception-safe code? 
Explain about exception Neutrality ?
Explain how we implement exception handling in C++  
Explain terminate() and unexpected() function  ?
Exception handling concept  ?
Describe the way to handle run time error in C++.