Skip to main content
gamedev.net
🔒 Locked

catching an error

Started by guyaton Oct 27, 2005 at 9:16 PM 4 replies 400+ views
Original Post
guyaton
guyaton
What is the best way to test if a pointer is not successfully created? I've noticed that Microsoft in their DirectX SDK checks if the pointer is NULL, but isn't it possible for the "new" to throw an error as well? Which do i check for? both? I am using Microsoft Visual Studio .NET 2002. thanks in advance, ~guyaton
~guyaton
RabblePants
RabblePants
Quote:
Original post by guyaton
What is the best way to test if a pointer is not successfully created? I've noticed that Microsoft in their DirectX SDK checks if the pointer is NULL, but isn't it possible for the "new" to throw an error as well? Which do i check for? both? I am using Microsoft Visual Studio .NET 2002.

thanks in advance,

~guyaton


I just check for NULL, but if you really want to you can do a try/catch as well. Since I use exception handling very sparingly, I opt for the NULL check.
ender7771
ender7771
Before you check for NULL, be sure to initialize your pointer to null becuase some compilers dont automatically point a failed pointer to NULL:

var *ptr = NULL;

.
. // do stuff w/ pointer
.

if(ptr == NULL) // and so on..
Skeleton_V@T
Skeleton_V@T
If newing a object failed, it throws an exception according to recent CPP standards. You can use 'placement new' to specify the Run time lib not to throw an exception but return a NULL value instead (the old way).

By default, new throws an exception when failed so you have no reason to check for NULL. Google it with 'Placement new' for further infos.
This is a good place to start
--> The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones <--
Sneftel
Sneftel
Quote:
Original post by Grafalgar
I just check for NULL, but if you really want to you can do a try/catch as well. Since I use exception handling very sparingly, I opt for the NULL check.

To expand on Skeleton's post: Unless you're using the nothrow placement-new, you can't "opt for the NULL check". If new decides to throw an exception and you aren't in a try-catch block, your program crashes immediately, regardless of whether you're planning to check for null.
guyaton
guyaton
thanks all. I just was not sure how MS did it in VS.NET. guess I'll stick with my try...catch blocks.

thanks!
~guyaton

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.