Click here to Skip to main content
15,886,075 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all..

I am writing a code in c++. All I want to do is catch all the exception that can possibly occur in the code. I am using try-catch block for that.
My problem is the code does not reach the catch block until and unless I give an explicit throw in the try block. For doing this I have to check each and every pointer that I am using within the code.
What I want to know is, is there a way that whenever any exception occur in my try block the control automatically gets transferred to the catch block without explicit throw(as is done in java).

Can anyone please help me with this ?
Posted

Note that Microsoft differentiates between C++ exceptions (as defined by the ANSI standard) and Structured Exceptions (as defined by MS and independent of C++). An access violation produces that latter one.

To catch structured exceptions you can use the __try / __finally mechanism as explained in solution 1.

You can also mix both types of exceptions by using the _set_se_translator function and compiling with /EHa. This mechanism helps you produce a C++ exception for every structured exception.
 
Share this answer
 
You are doing it correctly.
Dereferencing a null pointer in C++ is undefined behaviour and so no exception is thrown in c++ (unlike Java).

If using VS >= 2008 then it is possible to use try-except Statement

http://msdn.microsoft.com/en-us/library/s58ftw19%28v=VS.90%29.aspx[^]

From experience checking yourself for a valid pointer is the best approach.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 10-Jul-13 10:39am    
Good point, 5ed.
—SA
It simply means that you did not actually catch all exceptions. This is how you could do it:
C++
try{
    // ...
} catch (...) {
    // ...
}


This approach is often criticized, for some good reasons. Unlike systems where all exceptions are always derived from one single exception type (so it can be used to catch all possible exception and to still have an exception object), in C++ this is not the case: you can through absolutely any object, including a primitive-type one. When you catch some unknown exception, you won't know what to do with it.

So, you can use this construct in two cases: 1) if you want to block propagation of all exception, which is a pretty bad trick, which can be unavoidable in some "bad" situations, for example, to compensate some defects in bad APIs which are not accessible for patching for your; 2) for pure debugging/research purposes.

The second option can help you well. You can put a break point of the catch block shown above, create some exceptional situation, such as integer division by zero, and see what type is caught. Then you can add additional catch blocks designed to catch some particular types of exception object.

Remember some of the main principles of exception handling: reduce catching to minimum, don't try-catch locally, let go, handle exceptions in the points of the stack where the code is competent to handle it (I call it "points of competence"), at the same time, always handle all exception somewhere on the top stack frame of the stack of each thread.

—SA
 
Share this answer
 
v2
Comments
[no name] 10-Jul-13 4:07am    
That won't work for dereferencing null pointers. For example:

int *p = 000000;
*p=1;

This is not caught because no exception thrown.
Sergey Alexandrovich Kryukov 10-Jul-13 10:39am    
Yes, unfortunately you are right. I saw you answer, up-voted it. Your advice to avoid the situation is the best one can do.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900