Sign in to follow this  
GenuineXP

C++ Exceptions

Recommended Posts

Hello. I was just wondering about how standard C++ exceptions are. I've read that exceptions are the primary method that the C++ standard library handles errors. I'd like to use exceptions in my own code, but I'm worried it may not be supported elsewhere or won't comply with most IDEs\compilers. I'm sure exceptions are a concrete and well supported feature of C++ at this point, but I just want to be sure before I add them to my code. They seem to make dealing with certain tricky situations far easier. More specifically, I want to use them in a few situations where I want to check whether or not a pointer is still pointing to an object that hasn't been deleted. Thanks!

Share this post


Link to post
Share on other sites
Well they are part of the C++ standard so any standards conforming C++ compiler should support them. Unless you're planning on using some really old compilers you shouldn't have a problem. Also note exceptions are a language standard, so that the actual implementation is left up to the compiler writers, this means you shouldn't do things like throw exceptions across DLL boundries (unless the compiler both the DLL and program using it are the same and you don't mind if it breaks if someone compiles the DLL or program source with another compiler).

Share this post


Link to post
Share on other sites
Quote:

They seem to make dealing with certain tricky situations far easier. More specifically, I want to use them in a few situations where I want to check whether or not a pointer is still pointing to an object that hasn't been deleted.


Writing solid, exception-safe code isn't easy, however. It's more than wrapping everything in try/catch blocks everywhere.

Additionally, exceptions should be used for exceptional situations -- generally, errors that cannot be recovered from at the current point of execution. They should not be used for flow control in lieu of, say, an if/else construct (it sounds like your example of checking for "invalid" pointers might be a bit of a misuse of the exception mechanism).

Futhermore, unless you are implementing your own heap tracking and/or allocation system, you cannot, given an arbitrary memory address P, determine if P points to valid, allocated memory (unless P == 0) without using OS-level (and thus, nonstandard and nonportable) APIs (if that is even possible; generally its not), which makes what you seem to want to do rather difficult.

Share this post


Link to post
Share on other sites
Quote:
Original post by jpetrie
Futhermore, unless you are implementing your own heap tracking and/or allocation system, you cannot, given an arbitrary memory address P, determine if P points to valid, allocated memory (unless P == 0) without using OS-level (and thus, nonstandard and nonportable) APIs (if that is even possible; generally its not), which makes what you seem to want to do rather difficult.


Quoted for emphasis.

Beyond that, you should read this.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this