Sign in to follow this  
dmatter

SlimDX - Direct3D constructor and exceptions.

Recommended Posts

Does Direct3D9.Direct3D have a constructor that throws? As far as I can make out the documentation indicates there is no explicitly defined constructor. The sample framework, however, seems to imply that simply constructing this class might throw an exception (see GraphicsDeviceManager.EnsureD3D9() and DeviceSettings.FindValidSettings()). Is there a way to determine whether a method throws? I was unable to find an example in the documentation where it states whether a given method will or will not throw some exception. Thanks for any help.

Share this post


Link to post
Share on other sites
I'll let Mike speak to the sample framework issues, I just have a comment on the exception issue.

In general, you can safely assume that methods and property setters might throw and that property getters will not. In a perfect world, our documentation would accurately list the exception we might explicitly throw out of the function, but that doesn't mean those are all the exceptions that could throw -- in particular the runtime could throw an exception for us, or a method we call might throw nothing in one version, but something in another version, which we may fail to know about since we don't re-review the documentation for every method we ever call on every update. Exception documentation should always be regarded as highly volatile and possibly inaccurate because it is derived entirely from human observation -- the language provides no mechanism to tell us the set of exceptions that may be thrown from a given method (unfortunately).

[Edited by - jpetrie on October 22, 2008 11:19:03 AM]

Share this post


Link to post
Share on other sites
The Direct3D does in fact have an explicit constructor, one which can throw an exception when D3D or D3DX cannot be found, which is what I'm trying to catch when I call EnsureD3D9().

This is what the method looks like:

Direct3D::Direct3D()
{
IDirect3D9 *direct3D = NULL;

try
{
direct3D = Direct3DCreate9( D3D_SDK_VERSION );
}
catch( System::Runtime::InteropServices::SEHException^ ex )
{
throw gcnew Direct3D9NotFoundException( "Direct3D 9 was not found. Reinstalling DirectX may fix the problem.", ex );
}

if( direct3D == NULL )
throw gcnew Direct3D9Exception( "Could not create Direct3D instance." );

adapters = gcnew AdapterCollection( direct3D, false );

try
{
D3DXCheckVersion( D3D_SDK_VERSION, D3DX_SDK_VERSION );
}
catch( System::Runtime::InteropServices::SEHException^ ex )
{
throw gcnew Direct3DX9NotFoundException( "Direct3DX 9 was not found. Please install "
"the latest DirectX end-user redistributable package from Microsoft.", ex );
}

Construct( direct3D );
}




Our documentation is probably our biggest shortcoming right now. It's certainly not complete, although we're definitely working on it.

Share this post


Link to post
Share on other sites
Okay, thanks guys, that's rather helpful to know.

So, in general then, to manage exceptions from SlimDX I'll just catch System.Exception and then perhaps rethrow it wrapped up in a custom exception class depending on what is appropriate.

If I were interested in catching an exception thrown explicitly by SlimDX then is it SlimDXException I should be catching since I can't assume you throw a more specific subclass? I can't think right now of any reason to determine whether an exception was thrown by SlimDX itself or by the runtime, but it might be good to know later.

Share this post


Link to post
Share on other sites
I wouldn't advocate catching Exception in general -- I only do this as a last-ditch measure at the highest possible scopes in order to prevent the application from actually crashing on an unhandled exception (typically only in GUI apps as well). In general, catch the most specific type you can.

We will throw exceptions specific to each subsystem -- if you are using D3D9, you will get Direct3D9Exceptions, for example. Those exceptions indicate an error in the D3D machinery, either internally or because you did something you should not have. We will also throw argument-null exceptions in some cases, which are generally considered programmer errors, et cetera.

Share this post


Link to post
Share on other sites
Quote:
Original post by jpetrie
We will throw exceptions specific to each subsystem -- if you are using D3D9, you will get Direct3D9Exceptions, for example. Those exceptions indicate an error in the D3D machinery, either internally or because you did something you should not have.
Ah of course, so it seems that is the most specific type of exception I can reasonably catch without knowing which exceptions a method can throw. I'm more than happy to catch those instead of just Exception. [smile]

Quote:
We will also throw argument-null exceptions in some cases, which are generally considered programmer errors, et cetera.
Hmm, is this thrown by all methods where passing a null argument is obviously stupid? Or is it a bit hit and miss as to which methods would throw this? I'm happy catching it at a higher level if it can't be known/guessed which individual methods are likely throw this.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this