If I do not care about a thrown Exception. Can I code in a way as not to create it?
In other words, can an Exception be thrown but no Exception object be created?
An example – Simple example using a System.Net.Sockets.Socket
Socket acceptingSocket;
acceptingSocket.Blocking = false;
while(condition)
{
try
{
Socket acceptedSocket = acceptingSocket.Accept(); //(i)
doWork(acceptedSocket);
}
catch{}
}
because the socket is in non-blocking mode
if there is no connection to accept
a SocketException is thrown at point (i)
and the conditional loop continues.
using this code implementation – will the SocketException object be created?
AND if it is created – is there a way to NOT create it?
You can’t suppress exceptions, you can only ignore them. In any case, it’s not the creating of the Exception object that’s expensive — it’s the stack walk that happens when the exception is thrown.
Also,
not
🙂