2

I have this code in ASP.NET form:

bool cont = true;
int i = 0;

while (cont)
{
    try
    {
        JToken token = tempJSON["temperature"]["tool" + i];
        temperature.Add(JsonConvert.DeserializeObject<Structures.Temperature>(token.ToString()));
        i++;
    }
    catch(NullReferenceException ex) { pokracovat = false; }
}
 
return new TemperatureAndState(temperature, state);

After running code, the program raises a NullReferenceException inside try catch. How it is possible. I thought that if an exception is raised in a try block the program will call catch and then continue.

Error:

error image

TarHalda
  • 1,050
  • 1
  • 9
  • 27

1 Answers1

0

When debugging, the compiler will stop and Visual Studio will break on the error that occurred, even though it has a catch.

Don't worry, this will never happen when running the application normally.

Note: If the exception thrown inside your try block is not handled in the catch block, the application will crash, so it'd be great if you add a catch(Exception) block, so if a non-prevented exception occurs, you treat it accordingly.