C++ Exceptions
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!
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).
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement