I am trying to check a character pointer is null .How to check if the value is null i am basically from java
char* mypath = getenv("MYPATH");
if(!mypath) //this is not working
throw "path not found";
if(mypath == NULL) //this is also not working
throw "path not found";
i am getting an exception “terminate called after throwing an instance of ‘char const*'”
The problem isn’t the test: both of your
ifare correct (as far as the compiler is concerned—the second is preferable for reasons of readability). The problem is that you’re not catching the exception anywhere. You’re throwing achar const[13], which is converted to achar const*as the type of the exception, and you don’t have acatch ( char const* )anywhere in the calling code.In C++, it’s usual (but not at all required) to only throw class types derived from
std::exception; about the only exception to this rule is to throw anintfor program shutdown, and then only if that is the documented convention in the context where you work. (It only works correctly ifmaincatchesintand returns the value.)