Archived

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

try/catch not working in c#, getting crazy

This topic is 5012 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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 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 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