Archived

This topic is now archived and is closed to further replies.

brann

try/catch not working in c#, getting crazy

Recommended Posts

hi all i''m getting mad, here''s my code : try { device.Clear(ClearFlags.Target, Color.BlueViolet, 1.0F, 0); } catch { return; } When it executes, i get a System.NullReferenceException on the device.clear instruction ... I''ve also tried to catch this specific exception, without success What am i missing??

Share this post


Link to post
Share on other sites
I youre under MSVC.NET then go to Debug->Exceptions and check that Common Language Runtime Exceptions are set to continue.

Share this post


Link to post
Share on other sites
actual settings are to break only i the exception is not handled

btw, it works fine in a lot of other try/catch

Share this post


Link to post
Share on other sites
That means device itself is null, I don''t think try-catch will catch that exception, only exceptions within device.clear itself.

Share this post


Link to post
Share on other sites
i have added this to my code :

if (device ==null)
return;

it''s failing all the if test with a null pointer ex....

there''s definitely something i''m missing. how is that i cant even test device?

Share this post


Link to post
Share on other sites
quote:
Original post by Fidelio66
That means device itself is null, I don''t think try-catch will catch that exception, only exceptions within device.clear itself.

This is wrong.

Share this post


Link to post
Share on other sites
quote:
Original post by brann
i have added this to my code :

if (device ==null)
return;

it''s failing all the if test with a null pointer ex....

there''s definitely something i''m missing. how is that i cant even test device?



You can''t do ANYTHING with a null reference, except test to see if it is null, assign it a different reference, or assign it to another reference ... ANY function call or dereferences will cause an exception to be thrown (although I don''t know why your try catch doesn''t catch it).

Now I have no idea WHY your device is null ... but that''s elsewhere in your program, before the lines of code you posted.

Share this post


Link to post
Share on other sites
arild : yes i''m pretty sure it''s the good line
i''m using visual studio .net, and this line is in green with a yellow arrow in front of it ....

xai : you are off topic, my problem is the try/catch thing, not the fact that device is null.

Share this post


Link to post
Share on other sites
arild: hum you''r right, i mislooked something :

An unhandled exception of type ''System.NullReferenceException'' occurred in microsoft.directx.direct3d.dll''

but i dont know where i''m calling this dll, since i''m only testing (device == null), and device is private Device device;

Share this post


Link to post
Share on other sites
some new info on the problem

the problem occurs when the users hits the escape key, causing the mainloop to execute Application.Exit();
i''ve steped that in the debugger, and after this application.Exit, the program keeps executing the main loop, and in the render method, the test on device fails

i think Application.Exit() has GCed some classes, and that''s why i get nullpointerex when i try to access some variables....

but WTF.. isnt Application.Exit() supposed to free stuff, and then exit ?

Share this post


Link to post
Share on other sites
When you call Application.Exit your form is closed, and it's DirectX device is lost, so it's best to check if the form is still valid in your rendering loop.

while(frm.Created)
{
frm.Render();
Application.DoEvents();
}

Just close the form on an Esc keypress

protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
{
if ((int)(byte)e.KeyChar == (int)System.Windows.Forms.Keys.Escape)
this.Close();
}


[edited by - Serge Asperge on March 22, 2004 12:20:32 PM]

Share this post


Link to post
Share on other sites