Reputation:
I recently started learning DirectX/Windows, and the book I'm learning from had the code
d3d = Direct3DCreate9(D3D_SDK_VERSION);
if(d3d == NULL)
//catch error &c.
My question is: What would cause an error in this line, that is different than what would cause an error in another line (say, for example, int num = 42
)?
Upvotes: 0
Views: 250
Reputation:
It's important to catch that error because it's a show-stopper and because it can and does happen. As for other errors - I don't know what other errors you're talking about.
Certainly all errors should be checked for and handled. But remember that in a book, the priority is to make the key points as readable and understandable as possible. Complete error checking would get in the way of that, so it is often left as that infamous exercise for the reader.
Upvotes: 0
Reputation: 22624
d3d = Direct3DCreate9(D3D_SDK_VERSION);
if (d3d == NULL)
This is an error or not according to the meaning you give to the return value of Direct3DCreate9, i.e. depending on the specification of the function. I've written many pointer-returning functions for which NULL as a return value was not an erroneous situation.
So, do not equate "a function returning NULL" to "an error". An unambiguous error is a crash (technically, undefined behaviour) in your code, like if d3d is indeed NULL and later you dereference it.
int num = 42;
Here you are declaring an int variable called num and initializing it with a value of 42. What kind of error can you think of? Obviously, num will never "be NULL", if that bothers you. 42 may be a correct value or an error, depending on the context.
Upvotes: 4
Reputation: 1365
I think the comment is indicating that if Direct3DCreate9(D3D_SDK_VERSION);
has returned NULL, then it is an error that should be handled.
From msdn:
IDirect3D9 * Direct3DCreate9( UINT SDKVersion );
Parameters
SDKVersion The value of this parameter should be D3D_SDK_VERSION. See Remarks.
Return Values
If successful, this function returns a pointer to an IDirect3D9 interface; otherwise, a NULL pointer is returned.
It is not saying that the comparison d3d == NULL
might throw an exception.
Upvotes: 2