I am pretty new to C#. I am currently writing a WebSocket application, and I need to handle the NullReferenceException when the client disconnects, as I am constantly reading data from ClientSocket.
So the trouble is: When I place the second try-catch block inside the first one, I am able to catch the NullReferenceException. But when I remove the nested try-catch and try to catch the mentioned exception, it goes straight to the "finally" block.
try
{
using StreamReader streamReader = new StreamReader(stream);
while (true)
{
try
{
command = streamReader.ReadLine().Trim();
}
catch (NullReferenceException)
{
blah-blah
break;
}
}
}
//I place the NullReferenceException when removing the nested try-catch
catch (Exception e)
{
blah-blah
}
finally
{
blah-blah
}