Reputation: 5088
Is it possible to let C++ throw a NPE when calling a method on a nullptr object, instead of going in undefined behaviour? I could create a handler for a SEGFAULT signal but this would be realy dangerous, because not every SEGFAULT is a NullPointerException. If i have to do it via just checking in an if clause, is there a efficiant way to do so? Maybe also on compiletime?
Upvotes: 2
Views: 3687
Reputation: 670
Another way of exception handling: calling a function using NULL pointer
#include <iostream>
#include <typeinfo>
using namespace std;
char str_NullPointer[25] = "NULL Pointer exception";
char str_Arithmetic[25] = "Arithmetic exception";
class A
{
public:
int i = 20;
public:
int function()
{
printf("Function start\n");
try
{
if (this)
{
printf("value of i = %d \n", i);
}
else
{
throw str_NullPointer; /* Exception thrown in Case 2 */
}
}
catch(const int s)
{
printf("%d\n", s);
}
catch(const char* s)
{
printf("%s in %s\n", s, typeid(this).name()); /* Exception and Class Name */
}
printf("Function end\n\n\n");
}
};
int main() {
//code
printf("Case 1: With Pointer\n");
A *obj = new A();
obj->i = 20;
obj->function();
printf("Case 2: With NULL Pointer\n");
delete obj;
obj = NULL;
obj->function();
return 0;
}
Output:
Case 1: With Pointer
Function start
value of i = 20
Function end
Case 2: With NULL Pointer
Function start
NULL Pointer exception in P4abcd
Function end
Upvotes: 0
Reputation: 264621
Yes you can but its not really a good idea (you should not be handling pointers anyway, in modern C++ pointers are held inside objects that manage their lifespan).
You can always define a class that holds the pointer. Then when you try and use operator->()
it will throw if the held pointer is nullptr
.
template<typename T>
class ThrowingUniquePtr
{
T* ptr;
public:
// STUFF to create and hold pointer.
T* operator->()
{
if (ptr) {
return ptr;
}
throw NullPointerException; // You have defined this somewhere else.
}
};
class Run
{
public:
void run() {std::cout << "Running\n";}
};
int main()
{
ThrowingUniquePtr<Run> x(new Run);
x->run(); // will call run.
ThrowingUniquePtr<Run> y(nullptr);
y->run(); // will throw.
}
Upvotes: 2