r/programminghelp • u/[deleted] • Oct 06 '22
C# How would an Exception end the program despite being caught?
So I'm getting a "NullReferenceException", and visual studio highlighted the line "o = LocationsInViewport[y, x].ObjektsInLocation[0];", which is ironically in a try block. My point isn't to ask you to fix my entire program, unlike what a few condescending assholes who replied on stackoverflow assumed. I'm just asking how a "NullReferenceException" would end the program, despite the exception being caught. I've ruled out the possibility of the wrong line being highlighted by dividing the function into different try-catch blocks, as shown below.
public void UpdateDisplay()
{
Objekt o = null;
string GraphicName = " ";
Texture2D GraphicToDisplay;
try
{
Console.WriteLine($"Radius: {VIEW_RADIUS}");
LocationsInViewport = UpdateVisibleLocations();
}
catch (NullReferenceException)
{
Console.WriteLine("Test");
}
try
{
for (int y = 0; y < VIEW_RADIUS * 2; y++)
{
for (int x = 0; x < VIEW_RADIUS * 2; x++)
{
try { o = LocationsInViewport[y, x].ObjektsInLocation[0]; }
catch (NullReferenceException)
{
Console.WriteLine("Test2");
Thread.Sleep(8743843);
}
if (o != null)
{
try
{
GraphicName = ObjektGraphixReferences[o.TypeID];
GraphicToDisplay = Content.Load<Texture2D>(GraphicName);
int MonoGameXPos = x / (VIEW_RADIUS * 2) * Nulandia.SCRN_WIDTH;
int MonoGameYPos = y / (VIEW_RADIUS * 2) * Nulandia.SCRN_HEIGHT;
o.SpriteDestinationRectangle = new Rectangle(MonoGameXPos, MonoGameYPos, o.WidthOnScrn, o.LengthOnScrn);
Nulandia.spriteBatch.Draw(GraphicToDisplay, o.SpriteDestinationRectangle, Color.White);
Console.WriteLine($"Position of object relative to screen: ({o.SpriteDestinationRectangle.X}, {o.SpriteDestinationRectangle.Y}");
}
catch (NullReferenceException)
{
Console.WriteLine("Is null.");
Thread.Sleep(8743843);
}
}
}
}
}
catch(NullReferenceException)
{
Console.WriteLine("test");
Thread.Sleep(8743843);
}
}EDIT: It was solved, for whoever else uses Visual Studio 2022 and has this issue, go to Debug > Windows > Exception settings, then find "Systems NullReferenceException" under "Common Language Runtime Exceptions" and make sure that the setting is switched off. Apparently it'll ignore the "catch" block and just end the program.
1
u/dudefunk49 Oct 07 '22
My thought is not handling non explicit exceptions with a global exception handler that captures the stack trace... Aka. ELMAH if that's still around. It's been a while
1
1
1
3
u/dudefunk49 Oct 07 '22
That's you and the IDE. you're not talking production weirdnees then. I'm glad