C++

Topic: Exceptions

Explain about exception Handling Philosophies with example ?

The fundamental reason for throwing an exception is to stop program execution along the current path. This generally means an error condition has occurred and normal program flow cannot continue. The best benefit to this is that client code can be simplified and yet still allows the implementer of a component to guarantee that if an error occurs the client will be aware of it. Basically, it removes the responsibility of a client to check error codes.There seem to be three common reasons for throwing an exception:    A programming error - a pointer is null when it should not be    A resource is not available for acquisition or release - bad_alloc on memory exhaustion    A violation of a class invariant or a state of an object invariant - such as an invalid parameter is passed to a method or a calculation that goes out of rangeSo the reasons to throw an exception seem pretty straightforward. The more difficult question is where to handle exceptions. This is can be a lot more complicated an issue because of the flow control aspect of exceptions. We have to look at what most try/catch blocks are trying to accomplish.Generally, I've seen try/catch blocks trying to do a few different things:    Prevent resource leaks    Stop an operation after it has failed    Stop an operation deciding how to continue based on the cause of the errorAs we've seen from "Resource allocation is initialization" the first use of a try/catch block is completely unnecessary. We can eliminate the need for the try block, improving the clarity of the code, by using auto_ptr and similar classes to handle the releasing of resources in their destructors. This can also work for thread synchronization objects ( at least ones which you are not going to timeout on, such as a critical section ) and can be applied to other forms of resource acquisition. So, we can easily, relatively speaking, remove the try/catch blocks which are only preventing resource leaks and improve program performance and clarity.The second use of a try/catch block is probably the most coherent use of exception handling. At some level in the system we attempt to perform a, usually, complex task which may fail in a number of ways. In this use of a try/catch block we are only concerned with complete success. No matter what the failure is we handle it in the exact same manner. Informing the user of the cause of the error can still be handled in a generic way, particularly if you have your own exception hierarchy. Your base class of your exceptions can provide a method for translating an exception into a human readable message that can be presented to the user. This allows the user to know why the operation failed and yet keeps the flow control simple in the error handler case.The last example of exception handling seems at first like a simple permutation on the second one, but there are number of consequences involved. Having more than one catch block amounts is a form of branching and introduces more complex flow control. There are instances where this is unavoidable but hopefully you can minimize them. Maybe you handle only a few specific exceptions and the others are not handled. Perhaps these are from an unrelated exception hierarchy or you must handle specific exception classes in unique ways.The critical question to consider is whether or not the system is handling the exception at the given location. Handling the exception can usually be read as not re-throwing it. If you are re-throwing an exception then you're not really handling it, you're just trying to return your object or system to a valid state. And if you find yourself having to do this in a number of different ways depending upon the exception thrown then you are potentially creating future maintenance problems. The reason for this is that it implies that proper error handling follows a chain of exception handlers that are spread across different levels of the system.To minimize this complexity you should let the exceptions go up to the highest level possible in the system, usually the point at which the application, or highest level components of the package, initiated the operation.Similarly, a thrown exception should always propagate outside of the function which generated the exception. They should also usually propagate out of the component which generated them. This follows the idea that exceptions are a non local form of error handling. If you throw an exception in within a function and handle it directly in that function then it is being used as a form of flow control and can obscure that flow.

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++.